Last active
February 7, 2021 20:43
-
-
Save u-mulder/5c3762c40ce404a13ecc09b5a8b798ce to your computer and use it in GitHub Desktop.
Adding markup to php data
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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