I've been working on PHP
for some time but today when I saw this it came as new to me:
if(preg_match('/foo.*bar/','foo is a bar')):
echo 'success ';
echo 'foo comes before bar';
endif;
To my surprise it also runs without error. Can anyone enlighten me?
Thanks to all :)
That style of syntax is more commonly used when embedding in HTML, especially for template/display logic. When embedded this way, it's a little easier to read than the curly braces syntax.
<div>
<? if ($condition): ?>
<ul>
<? foreach($foo as $bar): ?>
<li><?= $bar ?></li>
<? endforeach ?>
</ul>
<? endif ?>
</div>
Versus:
<div>
<? if ($condition) { ?>
<ul>
<? foreach($foo as $bar) { ?>
<li><?= $bar ?></li>
<? } ?>
</ul>
<? } ?>
The verbose end tags make it a little easier to keep track of nested code blocks, although it's still mostly personal preference.