tags:

views:

48

answers:

3

I've been working with PHP code that generates HTML without any templating, and it's quite spaghetti and difficult to read with the way they've structured it. One way to drastically improve following the flow I've noticed is to use : endif instead of { } blocks in some cases to increase readability. (See http://php.net/manual/en/control-structures.alternative-syntax.php)

  • Is this portable?
  • Is this standard?
  • Is this slower in any significant way (I understand it takes more chars)

Cheers!

A: 

Personally, I detest them, but they're part of the language. They haven't been deprecated and I doubt they'll be removed anytime soon.

The thing you have to be careful about is that you can't mix the two syntaxes in the same control block (as the manual page says).

I don't find them any more readable. Identation gives enough clue. If you have blocks long enough that you see yourself using these control structures or } //end of while, you should refactor your code.

Artefacto
Agree 100%. I detest the comments on closing braces. If you need a comment, you're not indenting properly (or need to get an IDE, since it will do the folding for you)...
ircmaxell
Disagree. For HTML using curly braces is a maintenance fail. Good luck w/ the comment workaround.
Kristopher Ives
@Kris I was arguing **against** the comment work around. My argument is that indentation is the way to go.
Artefacto
A: 

to answer directly: Yes, it is standard and portable and it isn't any slower. It is a simple alternative to the standard curly-brace syntax. Whether or not you choose to use it is up to you as a style choice.

Personally, having been taught and used c-style languages my whole programming career (thus far) i prefer the curly braces with a simple comment after the end of the } curly brace that tells you what it is ending.

Scott M.
+1  A: 

Is this portable?

Yes

Is this standard?

Yes

Is this slower in any significant way (I understand it takes more chars)

I don't know.

But imho it increases readability and maintainability very much if you use it in combination with HTML.

Example:

<?php foreach($array as $value): ?>
    <div>
    <?php if($value == "foo"): ?>
        <p><?php echo $value; ?></p>
    <?php endif; ?>
    </div>
<?php endforeach; ?>

vs

<?php foreach($array as $value) { ?>
    <div>
    <?php if($value == "foo") { ?>
        <p><?php echo $value; ?></p>
    <?php } ?>
    </div>
<?php } ?>

It is just a small example but I think endif and endforeach are much easier to spot as <?php } ?>. So yes, definitely use it with HTML!

I don't use it anywhere else though because in "normal" code I find it more difficult to read.

Felix Kling