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.
2
-
If you are needing large blocks of html you can just write it outside of the ?> <?php blocksJames Culshaw– James Culshaw2013-07-25 10:58:34 +00:00Commented Jul 25, 2013 at 10:58
-
For blocks of code in PHP business logic, you can store PHP in external snippets and load them in when you need them. For JavaScript, perhaps a JS template engine will help (e.g. Handlebars).halfer– halfer2013-07-25 11:01:11 +00:00Commented Jul 25, 2013 at 11:01
Add a comment
|
4 Answers
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>
1 Comment
halfer
I was just about to mention the colon approach for view layers - but you beat me to it with your edit. Good stuff! You'll need to swap
$bar to <?php echo $bar ?> of course though.