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?

9 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.

  4. I’m a wordpress plugin programmer. We’ve designed a plugin that will scrape
    web browsers email addresses in to the database without having to use their communication.

    I’m on the lookout for beta evaluators and also, since you
    actually are gaining awesome chunks of web-site visitors, I am
    entertaining the idea of you and your blog post.
    Were you still interested?

  5. My partner and I absolutely love your blog and find the majority of your post’s to be just
    what I’m looking for. Would you offer guest writers to write content
    for you personally? I wouldn’t mind producing a post or elaborating on many of the subjects
    you write with regards to here. Again, awesome website!

  6. Moreover highly skilled workforce with more experience and specialization will
    be able to bring in a new zeal to complete the contracts with success.
    The developments and inventions within the Plumbing Daly City systems have aided us to get water into our houses and offices,
    after which drain the waste securely and continuously without doing harm to the climate.

    Most businesses can’t operate with water leaking all over the floor.

Leave a Reply

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