PHP Syntax Questions

I’ve been hacking around with the WordPress conditional tags and I’ve started to get the hang of it. However, I have never really known much of PHP programming and I don’t know jack about the syntax at all. I’ve spent too much time aping existing code’s syntax and this inquiring mind would like a hand-out of definitive info once and for all. I’ll be using a few shortcuts with syntax but I hope I can be understood anyway.

First, we have the conditional. Is there an important difference between the following two examples?

< ?php if (condition) : ?> [HTML Output] < ?php endif; ?>

< ?php if (condition) { ?> [HTML Output] < ?php } ?>

WordPress also has a conditional function to determine a page by ID or by slug. For example: < ?php is_page('about-the-author') ?> indicates code related only to a Page that has the slug of “about the author.” My sidebar file has a chain of “if” statements (only two are shown in the example):

< ?php if ((is_page('about-the-author')) { ?> [HTML Output] < ?php } if ((is_page('colophon')) { ?> [HTML Output] < ?php } ?>

I suppose the syntax is right since it works, but is it good syntax?

5 thoughts on “PHP Syntax Questions”

  1. Using the if…endif statement is thought of as the “other” way of writing it. There is nothing wrong with either way but I am guessing that there could be some speed payoffs if you stick with the original { statement. But I could be completely off.

  2. From what I know, there’s no real important difference b/t the two types of if statements. The if {} form is by far more commonly used; it’s C’s and Perl’s (which PHP is descented from) way of declaring an if statement. The if…endif is more Visual Basic/VBScript-y, and if I had to guess was put into PHP to make it more friendly to those with VB backgrounds.

    As for the syntax on the last statement in your post, it looks perfectly fine to me.

  3. The curly braces { } are good for “backend” code, because it’s easier for programmers to read. We have fancy text editors that match up the braces and do code collapsing, so it just makes our lives easier. But for “frontend” code, i.e. templates, PHP-agnostics will say “what is this “}” thing for?” If it’s “endif;” they can at least maybe figure out that it’s the end… of the if.

    As for your syntax, it is slightly inefficient to have a series of if () { } if () { }, because there is never going to be a case where more than one of those is true… (you can’t be viewing two different pages at the same time). Thus, it would be better to do if ( is_page(‘first’) { // first stuff } elseif ( is_page(‘second’) ) { // second stuff } And so on. Basically, by using elseif for every condition after the first one, you prevent more conditions from being evaluated than need to be. As soon as a page matches, it outputs the code in its section, and then quits the whole if… elseif loop, which saves you some cycles.

Leave a Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>