A Tour of Decision Making Structures in PHP - IF Statements
(Page 3 of 5 )
Now let's examine a scenario in which a PHP script executes a section of code only if a certain condition is met using an if-statement. An if-statement is made up of the keyword if followed by the comparison within parentheses. The block of code to be executed is set apart by braces.
<?php $x = 2;
echo "Hello, World. <br />"; if ($x == 2) { echo "\$x = $x <br />"; } echo "Goodbye."; ?> |
The script first assigns the value of 2 to $x and then prints a greeting message. A logical comparison is made between $x and 2, which returns true. Because this comparison evaluates true, the script will process the braced code. The script then concludes by displaying a parting message.
If there's only one line of code to be executed under the conditional circumstance, the braces may be omitted. The code could be re-written to resemble:
<?php $x = 2;
echo "Hello, World. <br />"; if ($x == 2) echo "\$x = $x <br />"; echo "Goodbye."; ?> |
However, it is considered good practice and good form by the PEAR coding standards to still use the braces.
IF-ELSE StatementsThe if-statement only allows for a piece of code to be executed or skipped depending on the return value of a comparison. However, it can be extended with the keyword else to allow for an alternate section of code to be processed.
<?php $age = 25;
if ($age < 13) { echo "You are under 13."; } else { echo "You are 13 or over."; } ?> |
In this example, an if-statement checks to see if the value of $age is less than 13. Because $age was assigned the value of 25, the first section of code would be skipped and the second section would be executed, displaying the message "You are 13 or over."
Again, when only one line of code is used in a section then braces do not need to be used... but it is considered good practice to use them anyway.
<?php $age = 25;
if ($age < 13) echo "You are under 13."; else echo "You are 13 or over."; ?> |
Question-Colon OperatorA nice, compact way to write a short if-else statement is to use the ?: operator. Because it uses symbols instead of keywords such as if and else, some may find it difficult to start using and avoid it. However with some practice it does become easy to use and can shorten the length of your code considerably.
The previous example can be written as follows:
<?php $age = 25;
echo ($age < 13) ? "You are under 13." : "You are 13 or over."; ?> |
The first section of code (between the question-mark and the colon) will be used if the conditional evaluates true. The second section of code (following the colon) will be used if the evaluation is false. In the code, the statement ($age < 13) is tested and returns false so "You are 13 or over." is displayed.
Typically, the ?: is seen when used to assign a value to a variable.
<?php $a = 100; $numbtype = ($a % 2 == 0) ? "even" : "odd"; echo "The number $a is $numbtype.<br />";
// is equivalent to writing...
$a = 100; if ($a % 2 == 0) { $numbtype = "even"; } else { $numbtype = "odd"; } echo "The number $a is $numbtype.<br />"; ?> |
IF-ELSEIF StatementsSeveral conditional statements can be chained together using if-elseif statements.
<?php $weather = "rain"; echo "The weather forcast calls for $weather. "; if ($weather == "snow") { echo "You'd better bundle up! <br />"; } elseif ($weather == "rain") { echo "Be sure to bring an umbrella! <br />"; } elseif ($weather == "wind") { echo "Take the day off and fly a kite!<br />"; } else { echo "Who knows what to expect?<br />"; } ?> |
In the example, PHP will process the chain of if-elseif. Once the first true comparison has been found, PHP will execute the desired code block and then break out of the structure. No further conditions of the structure are tested.
Next: SWITCH-CASE statements >>
More Programming Basics Articles
More By bluephoenix