Saturday, February 28, 2009

PHP If Statment Fundamentals short form shorthand

PHP If Statment Fundamentals short form shorthand

by Bret I’ve been doing a lot of work over the past year with PHP, and have been loving this language for dynamic web page development. I program in a couple of other languages so I have a few personal preferences for how I write code regardless of the language. Therefore, I often write standard if statements in PHP the same way I do in other languages; however, there are at least two other ways to write a PHP if statement that I often forget (old habits die hard) and am writing today’s post a quick “cheat sheet” type of reference for myself, and anyone else that needs a quick reminder of the available PHP if statement styles.

Please Note: All examples begin and end with the standard PHP preprocessing instructions: <?php and ?> However, in order to avoid these examples from appearing to the server as executable PHP code, I’ve intentionally inserted a space in these preprocessing instructions. In other words, “I meant to do that.”

The standard PHP if statement
PHP if statements work the same way as nearly all other programming languages: A conditional statement is evaluated and then additional code is, or is not, executed depending on the results of the conditional test:

< ?php

$number = 15;

if ($number > 10) {

echo "The number tested is greater than 10";

}

? >

No else section is necessary; however, if you need an else section, the format looks as follows:

< ?php

$number = 5;

if ($number > 10) {

echo "The number tested is greater than 10.";

} else {

echo "The number tested is less than 10.";

}

? >

Please note, while an if condition followed by a single line of then execution requires no curly brackets, I have shown my examples with curly brackets for readability and I believe it’s good programming practice.

If you need to extend your if statement but not by enough to justify using a switch statement, or a switch statement simply won’t work for a given situation, then add an elseif section:

< ?php

$number = 15;

if ($number > 10) {

echo "The number tested is greater than 10.";

} elseif ($number > 5) {

echo "The number tested is greater than 5 but less than 10.";

} else {

echo "The number tested is less than 5.";

}

? >

The abbreviated PHP if statement
The short form of the standard if statement drops the common if, else, and curly brackets to construct a condensed conditional statement that fits nicely on one line. The question mark character “?” separates the conditional section from the then section and then the colon character “:” separates the then and else sections. I often use this form in the header of some html documents that might be expecting a GET or POST values or when I’m checking for, and assigning, SESSION variables.

< ?php $username = (!empty($_SESSION['username']) ? $_SESSION['username'] : “”); ? >

I find using the abbreviated form to quickly test and set variables extremely valuable.

The extended PHP if statement
If you’ve ever examined the inner workings of a complex dynamic PHP and MySQL driven web page, then you’ve probably seen this extended form. This form allows you to contain the conditional, then, and else sections between their own preprocessor statements. The benefit is realized when you want to display a large chunk of html code for a then section and perhaps and equally large chunk for the else section. Normally, if you couldn’t “extend” the if statement in this manner, you’d have to write an awful lot of echo statements.

< ?php if ($number > 10): ?>

< table>

< tr>

< td>100< /td>< td>200< /td>< td> ...etc, etc < /td>

< /tr>

< /table>

< ?php else: ?>

< table>

< tr>

< td>10< /td>< td>20< /td>< td> ...etc, etc< /td>

< /tr>

< /table>

< ?php endif; ?>

To use this form, end the conditional statement with a colon “:” and enclose the statement in the standard preprocessor opening and closing tags. Write whatever chunk of html code you want to appear if the conditional criteria is met. Add an else section using the same format as the conditional statement section. Finally, end the entire if statement with an endif followed by a semi-colon and enclose it (same as the other sections) in the standard preprocessor opening and closing tags.

Keep in mind, it’s easy to get lost in an extended if statement because there is often a lot of code between the various sections; however, this form of the if statement can save a lot of typing because you won’t need to precede ever line you want printed with echo.

When to use each type of PHP if statement formats
There is no hard and fast rule about when to use each if format; however, for what it’s worth, here are my personal recommendations:

Standard: Use when there are only a couple of lines of code that follow a then or else section.
Abbreviated: Use when you want to assign a value to a variable based on a conditional statement and only have one line per then or else section.
Extended: Use when you have a significant amount of html code to display for each then and else sections. Be aware that it’s easy to get lost trying to following this format.

No comments: