Echoing HTML blocks in PHP is a pain, The echoed parts are not properly marked and parsed by IDE's since it's a string. This deficiency makes it very difficult to properly edit and change echoed html (especially with JavaScript). I wonder if there is an elegant solution except for using include in such cases.
问问题
617 次
4 回答
4
Yes, you don't need to echo just close PHP tags , e.g:
<ul>
<?php
foreach($foo as $bar) {
?>
<li> hi i'm <?=$bar;?> </li>
<?
}
?>
</ul>
EDIT:
for even more readability (often used in MVC views + wordpress type systems) omit the curly braces:
<ul>
<?php
foreach($foo as $bar):
?>
<li> hi i'm <?=$bar;?> </li>
<?
endforeach;
?>
</ul>
于 2013-07-25T10:59:24.877 回答
2
Here is another example using an alternative if-Syntax:
<?php if($a == 5): ?>
<p>A=5</p>
<?php endif; ?>
于 2013-07-25T11:03:47.687 回答
1
Just close and reopen the PHP tags
<?php
//blah blah PHP code
$title = 'My title';
?>
<section>
<h1><?php echo $title; ?></h1>
</section>
<?php
//More PHP code
于 2013-07-25T10:58:11.653 回答
1
You can open and close PHP tags (<?php ?>
) whenever you want:
<?php
//php code
?>
<!-- html goes here -->
<?php
//php code again
?>
于 2013-07-25T11:00:36.080 回答