Skip to content

Instantly share code, notes, and snippets.

@u-mulder
Last active February 7, 2021 20:43
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save u-mulder/5c3762c40ce404a13ecc09b5a8b798ce to your computer and use it in GitHub Desktop.
Save u-mulder/5c3762c40ce404a13ecc09b5a8b798ce to your computer and use it in GitHub Desktop.
Adding markup to php data
<?php
$newsArray = [
[
'title' => 'Новость номер 1',
'preview' => 'Краткое содержание номер 1',
'detail_url' => '/detail.php?id=1',
'author' => 'И. Иванов',
],
[
'title' => 'Вторая новость',
'preview' => 'Еще одно краткое содержание',
'detail_url' => '/detail.php?id=2',
'author' => 'П. Петров',
],
[
'title' => 'Новость 3',
'preview' => 'Краткое содержание номер три',
'detail_url' => '/detail.php?id=3',
'author' => 'Д. Дмитриев',
],
];
// Оптимальный вариант
foreach ($newsArray as $news) {?>
<div class="some-class">
<h2><?=$news['title']?></h2>
<p><?=$news['preview']?></p>
<a href="<?=$news['detail_url']?>">Читать текст целиком</a>
<span class="author">Автор новости: <?=$news['author']?></span>
</div>
<?php
}
// Вариант с конкатенацией данных
foreach ($newsArray as $news) {
echo '<div class="some-class">';
echo ' <h2>' . $news['title'] . '</h2>';
echo ' <p>' . $news['preview'] . '</p>';
echo ' <a href="' . $news['detail_url'] . '">Читать текст целиком</a>';
echo ' <span class="author">Автор новости: ' . $news['author'] . '</span>';
echo '</div>';
}
// Вариант с интерполяцией переменных в строках с двойными кавычками
foreach ($newsArray as $news) {
echo '<div class="some-class">';
echo " <h2>{$news['title']}</h2>";
echo " <p>{$news['preview']}</p>";
echo " <a href='{$news['detail_url']}'>Читать текст целиком</a>";
echo " <span class='author'>Автор новости: {$news['author']}</span>";
echo '</div>';
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment