It's possible to omit the terminating semicolon in a tag.


  <? foreach ($receivers as $receiver): ?>
    <td><?= $receiver->name ?></td>
    <td><?= $recevier->email ?></td>
  <? endforeach ?>

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.


3 Answers 3


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.


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).


I'm all for omitting them in this case, and I do exactly that, specifically because in this context I only use one single-line statement per or block. It reduces visual clutter and in all my years of doing this, has never resulted in a single issue.


Use only short tags

No. Please god no! Relying on short open tags is a terrible idea. It only makes your code less portable. Short open tags aren't enabled by default on many many systems. And what if you want to share the code to someone who has them disabled? Besides nothing is gained from short open tags IMHO.

Use only the alternative syntax for control structures

I fail to see what you would gain from that. Besides not having a consistent code base.

Have only one statement in a tag each

That might also make things messy. When you have some "template" cluttered with open / close tags everywhere.

Why not just agree in the team to use a limited subset of PHP as a template engine?

That depends who are on that team. E.g. will only people use it who know PHP? Or do you also have people on that team who need to change the templates who don't know PHP. If it is the first I would just relax on the requirements (a.k.a rules), because when people hit a problem they can easily solve with all the power PHP provides and you are going to limit them into solving it you would either have people breaking the rules or get very messy code. However if you have people in the team who don't know PHP you might get away with that just like that.

Now to answer you question:

However I read often that omitting terminating semicolons is a bad practice. Why? Is it a bad practice at all?

When coding (no matter what language and no matter what project) you always have to reduce the number of wtf's per minute in your code. So it is bad practise because when somebody looks at that they may think at first: hey something is strange there! It's just that PHP people are used that there are semicolons to close statements. So again this may be dependent of the people in your team (but you asked fer personal experience). Besides this it is also very very annoying to switch between syntaxes when people are working both on the frontend (the templates) as the backend. And is prone to both errors and annoyance in the team.

And finally about the fact that PHP allows you to do something: PHP allows you to do all sorts of things. Just this fact doesn't mean you should use it. There was a meta post once on Stack Overflow which about Stack Overflow not being the perfect example of its own rules (or something like that :) ) which could also be used for PHP. There is all sorts of FUBAR stuff in PHP (yes I've said it), but that shouldn't mean you should say: but but but PHP allows it. Guess what PHP also allows you to run queries without input sanitation. PHP also allows you to write all code on one single line. PHP also allows you to make variable names like $S0mEvAriBLe. All these things don't mean it is the best thing to do.

My 2 cents

  • Let me comment on your points: 1) Alternative syntax because HTML templating is a different realm than «normal PHP». I want to emphasize this with different constructs. 2) Only one statement in a tag because views are mostly HTML with only a little PHP code. 3) Limiting the power of PHP because of separation of concerns. In the views nothing about the application is decided, calculated or queried. It's the task of the controller together with the model. It would mess up the separation if someone uses the whole power of PHP in a view. Thank you for your answer. It is helpful.
    – nalply
    Aug 12, 2012 at 15:38

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.