Fork me on GitHub

article

Alternate Syntax for PHP Control Structures

November 17, 2005 | PHP Scripting

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!

5 Responses

Stay in touch with the conversation, subscribe to the RSS feed for comments on this post.

  • It seems Ruby has a similar option:

    Net::HTTP.start(url.host, url.port) { |http|

    results = http.post(url.path, data)

    puts results.body

    }

    can be

    Net::HTTP.start(url.host, url.port) do |http|

    results = http.post(url.path, data)

    puts results.body

    end

    looking at a bundle of ‘ends’ might be easier to follow than curly braces.

    James Badger, November 30, 2005 1:58 pm | permalink

  • Heh, beats using replace to count the number of braces to see if they match. Although having really good indenting in your editor of choice helps too.

    James Badger, November 19, 2005 6:43 pm | permalink

  • Really dude WHATS UP.

    Randy S. LIttle, November 29, 2005 9:48 am | permalink

  • I like curly braces better, I find them easier, but it’s cool to know that this other way exists. Nice find!

    Max, November 17, 2005 6:52 pm | permalink

  • This syntax is used quite a bit in WordPress’ default templates (and I believe elsewhere), and has been for a while. Get with it. 😛

    Joey B., November 17, 2005 2:41 pm | permalink

Comments are closed