如何格式化 PHP 中的长回显表达式?
How to format long echo expressions in PHP?
我正在 PHP 中编写我的函数以列出 post 个类别(只看代码风格,而不是逻辑):
function list_categories() {
// $html = '';
echo '<div class="post-categories post-items__categories">';
echo '<span class="post-categories__title">Categories:</span>';
echo '<ul class="post-categories__list">';
echo '<li class="post-categories__item"><a class="post-categories__link">All categories</a></li>';
$categories = get_categories([
'taxonomy' => 'category',
'type' => 'post',
'orderby' => 'name',
'order' => 'DESC',
'hierarchical' => false,
]);
foreach ($categories as $category) {
$cat_class = ($category->term_id == get_queried_object_id()) ? 'active' : '';
echo '<li class="post-categories__item">
<a class="post-categories__link" href="'.get_category_link($category->term_id).'">'.
$category->name.
'</a>
</li>';
}
echo '</ul></div>';
}
而且我希望我的标签回显表达式更具可读性,而不仅仅是像这里这样的一行:
echo '<li class="post-categories__item"><a class="post-categories__link">All categories</a></li>';
我在 foreach 循环中使用了新行,但它仍然很难看。
我发现了 heredoc 语法,但我无法调用其中的函数。
你怎么写这种东西?实在不想写成一行
如果你只需要回显 HTML 为什么不跳出 PHP,然后再返回
function list_categories() {
// $html = '';
echo '<div class="post-categories post-items__categories">';
echo '<span class="post-categories__title">Categories:</span>';
echo '<ul class="post-categories__list">';
echo '<li class="post-categories__item"><a class="post-categories__link">All categories</a></li>';
$categories = get_categories([....
变成...
function list_categories() {
// $html = '';
//break out of php
?>
<div class="post-categories post-items__categories">
<span class="post-categories__title">Categories:</span>
<ul class="post-categories__list">
<li class="post-categories__item"><a class="post-categories__link">All categories</a></li>
<!--carry on with php-->
<?php
$categories = get_categories([....
如果您需要在 html 代码中间调用变量,您可以这样..
<?php $demo = "hello"; ?>
<span><?php echo $demo; ?> World</span>
HTML 在变量中...
<?php $html = ' ?>
<!--HTML HERE-->
<?php '; ?>
我正在 PHP 中编写我的函数以列出 post 个类别(只看代码风格,而不是逻辑):
function list_categories() {
// $html = '';
echo '<div class="post-categories post-items__categories">';
echo '<span class="post-categories__title">Categories:</span>';
echo '<ul class="post-categories__list">';
echo '<li class="post-categories__item"><a class="post-categories__link">All categories</a></li>';
$categories = get_categories([
'taxonomy' => 'category',
'type' => 'post',
'orderby' => 'name',
'order' => 'DESC',
'hierarchical' => false,
]);
foreach ($categories as $category) {
$cat_class = ($category->term_id == get_queried_object_id()) ? 'active' : '';
echo '<li class="post-categories__item">
<a class="post-categories__link" href="'.get_category_link($category->term_id).'">'.
$category->name.
'</a>
</li>';
}
echo '</ul></div>';
}
而且我希望我的标签回显表达式更具可读性,而不仅仅是像这里这样的一行:
echo '<li class="post-categories__item"><a class="post-categories__link">All categories</a></li>';
我在 foreach 循环中使用了新行,但它仍然很难看。 我发现了 heredoc 语法,但我无法调用其中的函数。 你怎么写这种东西?实在不想写成一行
如果你只需要回显 HTML 为什么不跳出 PHP,然后再返回
function list_categories() {
// $html = '';
echo '<div class="post-categories post-items__categories">';
echo '<span class="post-categories__title">Categories:</span>';
echo '<ul class="post-categories__list">';
echo '<li class="post-categories__item"><a class="post-categories__link">All categories</a></li>';
$categories = get_categories([....
变成...
function list_categories() {
// $html = '';
//break out of php
?>
<div class="post-categories post-items__categories">
<span class="post-categories__title">Categories:</span>
<ul class="post-categories__list">
<li class="post-categories__item"><a class="post-categories__link">All categories</a></li>
<!--carry on with php-->
<?php
$categories = get_categories([....
如果您需要在 html 代码中间调用变量,您可以这样..
<?php $demo = "hello"; ?>
<span><?php echo $demo; ?> World</span>
HTML 在变量中...
<?php $html = ' ?>
<!--HTML HERE-->
<?php '; ?>