PHP Conditional Statements
It is common for scripts to evaluate conditions and change their behavior accordingly. These decisions are what make your PHP pages dynamic—that is, able to change output according to circumstances. Like most programming languages, PHP enables you to do this with an if statement.
There are several statements in PHP that you can use to make decisions:
• The if statement
• The if...else statement
• The if...elseif....else statement
• The switch...case statement
• The ternary operator
The if Statement
The if statement is a way of controlling the execution of a statement that follows it (that is, a single statement or a block of code inside braces). The if statement evaluates an expression found between parentheses. If this expression results in a true value, the statement is executed. Otherwise, the statement is skipped entirely. This functionality enables scripts to make decisions based on any number of factors:
if (expression) { // code to execute if the expression evaluates to true } if ( $age > 18 ){ print "Eligible for Driving License."; }
The if-else Statement
When working with an if statement, you might want to define an alternative block of code that should be executed if the expression you are testing evaluates to false. You can do this by adding else to the if statement followed by a further block of code:
if (expression) { // code to execute if the expression evaluates to true } else { // code to execute in all other cases } if ( $age > 18 ){ print "Eligible for Driving License"; }else{ print "Eligible for Driving License"; }
The if...elseif...else Statement
You can use an if...elseif...else clause to test multiple expressions (the if...else portion) before offering a default block of code (the elseifportion):
if (expression) { // code to execute if the expression evaluates to true } elseif (another expression) { // code to execute if the previous expression failed // and this one evaluates to true } else { // code to execute in all other cases } $d=date("D"); if ($d=="Fri") echo "Have a nice weekend!"; elseif ($d=="Sun") echo "Have a nice Sunday!"; else echo "Have a nice day!";
The switch Statement
The switch statement is an alternative way of changing flow, based on the evaluation of an expression. Using the if statement in conjunction with elseif, you can evaluate multiple expressions, as you’ve just seen. However, a switch statement evaluates only one expression in a list of expressions, selecting the correct one based on a specific bit of matching code. Whereas the result of an expression evaluated as part of an if statement is interpreted as either true or false, the expression portion of a switch statement is subsequently tested against any number of values, in hopes of finding a match:
switch (expression) { case result1: // execute this if expression results in result1 break; case result2: // execute this if expression results in result2 break; default: // execute this if no break statement // has been encountered hitherto } $d=date("D"); switch ($d) { case "Mon": echo "Today is Monday"; break; case "Tue": echo "Today is Tuesday"; break; case "Wed": echo "Today is Wednesday"; break; case "Thu": echo "Today is Thursday"; break; case "Fri": echo "Today is Friday"; break; case "Sat": echo "Today is Saturday"; break; case "Sun": echo "Today is Sunday"; break; default: echo "Wonder which day is this?"; }
The Ternary Operator
The ?: or ternary operator is similar to the if statement, except that it returns a value derived from one of two expressions separated by a colon. This construct provides you with three parts of the whole, hence the name ternary. The expression used to generate the returned value depends on the result of a test expression: (expression) ?returned_if_expression_is_true: returned_if_expression_is_false;
<?php echo ($number % 2 == 1) ? 'Odd' : 'Even'; ?>