PHP Tutorials – Conditional Statements
In this PHP Tutorials you will learn about Conditional Statements – if statement, if-else statement, Alternative if-else statement and switch statement.
if statement:
if syntax is as follows:
if (an expression that return Boolean value) {
Code to be executed.
}
PHP evaluates the Boolean expression to true or false, if the evaluation result is true, then the code inside the curly braces is executed, else it will be ignored.
Example:
<?php
$x = 5;
if($x < 10) { // returns true
echo $x; // Will be executed
}
?>
if-else statement:
if-else syntax is as follows:
if (an expression that return Boolean value) {
Code to be executed if the above expression returned true.
} else {
Code to be executed if the above expression returned false.
}
PHP evaluates the Boolean expression to true or false, if the evaluation result is true, then the code inside the if curly braces is executed, else the code inside the else curly braces is executed.
Example:
<?php
$x = 5;
if($x > 10) { // Returns false
echo $x; // Won’t be executed
}
else { // The else code will be executed
$x += 7;
echo $x; // Will print 12
}
?>
Many conditions can be checked with if-else statement, the syntax for using if-else with more than one condition is:
if (an expression that return Boolean value) {
Code to be executed if the above expression returned true.
} elseif (another expression that return Boolean value){
Code to be executed if the above expression returned false.
}
// Multiple elseif block can be added here
else {
Code to be executed if the above expression returned false.
}
PHP evaluates the Boolean expression for the if block first to true or false, if the evaluation result is true, then the code inside the if curly braces is executed, else it evaluates the next elseif blocks, if any of the elseif blocks returned true, then its code is executed, if no elseif condition returned true, then the else block is executed.
Example:
<?php
$x = 5;
if($x > 10) { // Returns false
echo $x; // Won’t be executed
}
else if (x == 5) { // The else code will be executed
$x += 7;
echo $x; // Will print 12
}
else { // Will be neglected
$x++;
echo $x;
}
?>
Alternative if-else statement:
An alternative way to write if-else statement is like the following:
condition? Value_if_true : Value_if_false
Example:
<?php
$x = 5;
$y = $x > 3? $x : $x + 5; // $y = 5
echo $y;
?>
switch statement:
if-else statement that needs more than one condition can replaced by case statement.
Example:
<?php
$x = 2;
if ($x == 1) {
echo ‘One’;
break;
}
else if (x == 2) {
echo ‘Two’;
break;
}
else {
echo ‘not one or two’;
}
?>
The previous code can be substituted with the following code:
<?php
$x = 2;
switch ($x) {
case 1:
echo ‘One’;
break;
case 2:
echo ‘Two’;
break;
default:
echo ‘Not one or two’;
}
?>
Example:
<?php
$x = 2;
if($x == 1 || $x == 2) {
echo ‘One or two’;
break;
}
else {
echo ‘Not one or two’;
}
?>
The previous code can be substituted with the following code:
<?php
$x = 2;
switch ($x) {
case 1:
case 2:
echo ‘One or two’;
break;
default:
echo ‘Not one or two’;
}
?>