PHP – Is Omitting Terminating Semicolon in a Tag a Good Idea?

PHPsyntax

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, and include 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.

Best Answer

I think it is a good idea to use PHP, or at least a subset of, as a template language. It will be easier for new developers to be integrated with your system as there's no special library syntax to learn and you don't have the overhead of that library parsing your template just to turn it into PHP.

Also, you missed a context in which you would need to eliminate the terminating semi-colon in your variable echo statements as well, e.g. <?= $receiver->name ?>. If you're going to not use semi-colons in your templates then you really need to not use them. Clearly your code example doesn't use them but I just wanted to point out the missing context in which not to use terminating semi-colons.

The really important thing is having team buy-in and ensuring consistency across your templates. Personally, I find it jarring to not have the semi-colon...I instinctively want to add those semi-colons into all your PHP statements. Do you have a person on your team with the same level of OCD? How will they react to this? If your team doesn't have buy-in and one or two people are vehemently against it, make them defend their decision, then maybe this isn't the best way to go.

Another potential pitfall would be incorporating new team members. They may very well not realize you can even do this, go back in your templates and insert all the terminating semi-colons where appropriate. Obviously this is a case of a developer not knowing as much about PHP as they should but it is something that you'd want to weigh in your decision. Educating new, junior developers that in templates we don't use terminating semi-colons but other code we do.

tl;dr

Get buy-in from your team, ensure your templates are consistent and have a reasonable plan to educate new developers on how PHP code in templates differ from "normal" PHP code (alternative syntax, no terminating semi-colon, allowed PHP structures, etc).

Related Topic