When, where & why should I use the "alternative syntax" for Control Structures?
Asked Answered
H

1

11

A simple PHP if / else condition looks like this:

if(true) {
  echo("Hello World!");
} else {
  echo("Goodbye World!");
}

The "alternative syntax" looks like this:

if(true):
  echo("Hello World!");
else:
  echo("Goodbye World!");
endif;

I have a few questions regarding this "alternative syntax":

  1. When / in what case should I use it?
  2. Where / in which part of my script should I use it?
  3. Why should I use it / what purpose does it serve & are there any benefits to using it?
  4. Why was it created, does the "alternative syntax" have features that "original syntax" doesn't?

Thank you for you time & help in advance.

Hedden answered 14/6, 2016 at 18:2 Comment(5)
I only use them on the rare occasion that I am using mixed php and html. The reason is it's much easier to match endif; or endforeach; then } or }. Basically it makes it a bit more readable when in the html. That said, like 99% of the time I use a template system and MVC structure.Wrack
1) Never. 2) No where. 3) You shouldn't. 4) I believe it was created to strike a balance between the myriad of languages available to us. PHP wants to be the go-to language so they decided to implement flexible control structures. The "alternative syntax" reminds me of PL/SQL to be honest. To me, PHP without curly braces is a suicide in progress.Beefeater
@Beefeater - I slightly disagree with 1, 2, and 3 but as I said in my comment, I use them in mixed HTML/PHP files, on vary rare occasions. Otherwise it's more readable to use the normal syntax.Wrack
Many good questions generate some degree of opinion based on expert experience, but answers to this question will tend to be almost entirely based on opinions, rather than facts, references, or specific expertise.Bewilderment
I totally agree with Jay Blanchard, and here is my opinion. I've used the 'alternative' syntax solely for 10+ years. I don't understand diehards like @Beefeater who says something like "we should all write in Klingon instead in English because, in my opinion, Klingon is a superior language!" Is it?Cerberus
W
21

I typically only use it when mixing HTML and PHP, so just for the sake of argument, here is an example to compare

<?php foreach( $vars as $var ): ?>
    <?php if($var):?>
      <p>Hello World!</p>
    <?php else:?>
      <p>Goodbye World!</p>
    <?php endif;?>
<?php endforeach; ?>


<?php foreach( $vars as $var ){ ?>
    <?php if($var){ ?>
      <p>Hello World!</p>
    <?php }else{ ?>
      <p>Goodbye World!</p>
    <?php } ?>
<?php } ?>

Granted this is small but if you extend this out about 800 lines it can get tricky to keep track of all those } having the endif; endforeach etc.. gives you just a bit better way to match the code blocks up when mixed with all the HTML content.

Again, I rarely use mixed HTML/PHP and I'd suggest using a Template engine if you have more then a few lines to do that mix the two. Because even this can wind up messy once you have a dozen endif; it loses it's point.

But, I guess in short I tend to think of it as a default template syntax...

Wrack answered 14/6, 2016 at 18:16 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.