PHP Quick Tip: When to use the alternative syntax

This video is ultimately in response to a tweet Harry Roberts posted earlier this week, whereby he said he disliked the PHP alternative syntax for control structures. I responded rather unhelpfully to Harry’s tweet so figured I should show you all how I use the alternative structure.

Truth be told, I must thank Harry, I’ve been wanting to talk about this for a long while.

 
  • Since tweeting that I learned it’s a templating/view convention to use the alternative syntax, which makes sense for simple views.

    The view I was working with, however, was pushing 1,000 lines and was *very* complex. This was a failing of the view, not the syntax. I’ve since started refactoring the code and it’s a little less unwieldy, but I guess my problem was that – when you have `if()`s spanning a hundred or so lines and interspersed with other loops and conditions – the lack of matching-brace-highlighting *really* sucks :(

    So yeah, it was the view‘s fault more than the syntax’s. Thanks for the deeper explanation :)

    H

    • No worries Harry, glad the topic came up either way, it’s been on my list for a while now :-)

    • Concerning the ‘non-matching-braces’ – either there should be a plugin for your editor or it can do it on it’s own. In vim for example you can jump between if and endif with % and a plugin called matchit: https://github.com/edsono/vim-matchit Sublime and others should have sth. like this too, i guess.

  • I totally agree with you. Using the alternative syntax in HTML templates is even in the Drupal coding standards http://drupal.org/coding-standards#controlstruct

    • Never realised they were in the Drupal coding standards, thanks for the heads up Alexis :-)

  • Well explained. In the video, maybe it would have been a good idea to show your example template using the standard syntax to highlight why it might be more difficult to read.

    • I concur Emyr, I totally should have done this. Thanks :-)

  • iansltx

    Couldn’t have said it better myself. When in Rome…er…an HTML context, do as the expected format does. If you’re using MVC, it would be silly to put the alternative syntax in the M and C parts, because you’re dealing with more-or-less pure code, maybe with some SQL stuck in your model methods. On the view, on the other hand, alternative templating systems (the big one for me being Twig/Flow) use a syntax that’s more or less the PHP alternative syntax, but with something else instead of . Because, in the context of HTML, that’s what you’re looking for.

    On the other hand, if you were templating out some JavaScript, you could go either way. The PHP open/close tags are reminiscent of HTML, but you’re outputting code rather than markup. But that’s a whole other can of worms, and heavily templated JS (as opposed to completely static JS files being included on a templated HTML page) is, from what I’ve seen, largely an edge case.

  • Nice video explanation.

    I like using PHP’s alternative syntax for exactly the reason you demonstrate in your video – it helps me to quickly identify what I am closing. I come from a front-end background initially so, as you explain in your video, I think the concept of explicitly closing what I open in HTML influences my approach to PHP/programming too (rightly or wrongly).

    I also go one further with my HTML and comment-close ambiguous elements, e.g.

  • Why can’t you just do something like


    foreach(a in b){
    Test
    } // endwhile or endif or endofreach

    (can’t get the syntax highlighting to work)

    It seems using ‘alternative syntax’ can get you in trouble. Did you find out if it works in straight php files? I guess if you are going to use it, you should use it consistently to make consistent readable code. I had no idea about the alternative syntax until I had to pick up someone else’s code.

    • beneverard

      You say you didn’t know about the alternative syntax before you had to pick up someone else’s code… how can you argue against it if you’ve not used it before?

      The alternative syntax will work in a straight PHP file… although I don’t think it should.

      The argument for using either the standard or alternative syntax consistently regardless of the files purpose doesn’t really stand up, mainly because a core PHP file won’t be dealing with the output HTML.

      Either way I’d recommend taking a look into it, in my experience using standard syntax in templates doesn’t make for readable code.

      Ben :-)

  • heyallanmoreno

    Still an awesome video to refer to +1