It's possible to omit the terminating semicolon in a tag.
Example:
<table>
<th><td>Name</td><td>Email</td>
<? foreach ($receivers as $receiver): ?>
<tr>
<td><?= $receiver->name ?></td>
<td><?= $recevier->email ?></td>
</tr>
<? endforeach ?>
</table>
Note the <? endforeach ?>
without a semicolon after endforeach
. The PHP Documentation says:
The closing tag of a block of PHP code automatically implies a semicolon; you do not need to have a semicolon terminating the last line of a PHP block.
Why am I bringing up this subject?
Currently I and a friend of mine work in a proprietary PHP project with MVC (model-view-controller). For the view I evaluated some PHP template engines like Smarty, Moustache and even designed my own home-grown template engine. But when I read that PHP is a template engine in itself something went «click» in my head. Why not just agree in the team to use a limited subset of PHP as a template engine? And having many code reviews to keep up the rules?
And this subset could look like this:
- Use only short tags
<?
,<?=
and?>
- Use only simple expressions without side effects, especially don't assign variables
- Use only
if
,foreach
, andinclude
for statements - Use only the alternative syntax for control structures
- Have only one statement in a tag each
This is the context where we are omitting the terminating semicolon, namely for <? endforeach ?>
, <? endif ?>
and <? include("list-contents.php") ?>
.
However I read often that omitting terminating semicolons is a bad practice. Why? Is it a bad practice at all?
I am happy that PHP allows omitting the terminating semicolon because this minimizes the visual clutter in the template files. They should look mostly like HTML files with only tiny PHP fragments here and there. The example above is what I am striving for.
What do you think about this? If you are against it: why? If you support it: Why would it be a good idea? What should I tell my partner if he is referring me some text against omitting the semicolon?
Please back up your opinion with facts, references or your own experience.