The Gippy Pages

article

Alternate Syntax for PHP Control Structures

Nov 17, 2005 | PHP Scripting | By Shawn Parker

I posted a link to this in my links section but get excited about it every time I use it, so I have to post a blurb on it here just in case any PHP geeks that read this didn’t click the link.

Its cool, dammit! Really, it is. No more curley braces for if, while, for, foreach, and switch statements!

C’mon – I know you’re interested.

So we’re all used to writing:

if($this == $that) {
    // do this
} else {
    // do that
}

That in and of itself isn’t too bad. But when we get into this:

if($this == $that) {
    if($this > 1) {
        // do other
    } elseif($this =< 1) {
        // wat?
    } else {
        // more
    }
} else {
    if($that != 0) {
        // do that
    } elseif($that > 0) {
        if($that < 10) {
            // whoa
        }
    } else {
        // panic
    }
}

It can get a bit hairy matching up all those braces.

I don’t know when this was added, but PHP has an alternate syntax for controlling these statements that eliminates the curley brace.

if($this == $that):
    // do this
endif;

We’re able to eliminate matching curley braces with an easier to find endif; statement. Now longer and deeply embedded statements get easier to make sure they’re closed. For example:

if($this == $that):
    if($this > 1):
        // do other
    elseif($this =< 1):
        // wat?
    else:
        // more
    endif;
else:
    if($that != 0):
        // do that
    elseif($that > 0):
        if($that < 10):
            // whoa
        endif;
    else:
        // panic
    endif;
endif;

Like I said, I don’t know when this was first introduced but I’ve never seen it taught in a book. The lowest version of PHP I currently work with is PHP 4.3.11 and it works fine. I suggest you give it a look see on your version (if older than that) to see if you can use it… I have no reason to think that you wouldn’t since there is no version caveat on the php page that explains using this but we all know to check first before depending on something, right? right?

So, hopefully this has been useful to ya, I know it has been for me!

Tagged as: alternate, brace, code, control, php, structure, syntax