PHP conditional logic
You can implement PHP code which makes decisions based on certain things using conditional logic.
This lesson focuses on:
- The if statement
- The else statement
- The else-if statement
- Using if, else, and else if together
- The switch statement
- The ternary operator
The if statement
The if statement tests if a certain condition is true or false and acts upon it accordingly.
Syntax:
if(condition){
perform this action;
}
If the condition in the parenthesis is true, then the code following the condition will be executed, otherwise it will not.
Example:
<?php
$aNumber = 5;
if ($aNumber == 5){
print "aNumber is equal to 5";
}
?>
Output:
NOTE: Use two equal signs (==) in the condition when comparing values. One equal sign (=) is used to assign values, while two equal signs (==) are used to compare values.
The else statement
If the condition in the parenthesis in an if statement is true, then the code following the condition will be executed. But what if you wanted something to happen if it is false? What if you wanted one thing to happen if the condition is true, and something else to happen if the condition is false? This is where the else statement comes in. The else statement works in conjunction with the if statement and executes certain code if the condition in the if statement is false.
Syntax:
if(condition){
perform this action;
}
else{
perform this action if the above condition is false;
}
If the condition in the if statement is false, then the action dictated by the else statement will be performed.
Example 1:
<?php
$plant = "cactus";
if ($plant == "cactus"){
print "Great choice!";
}
else{
print "Still a good choice, though cactuses
are better.";
}
?>
Output:
Example 2:
<?php
$plant = "birch";
if ($plant == "cactus"){
print "Great choice!";
}
else{
print "Still a good choice, though cactuses
are better.";
}
?>
Output:
The else-if statement
The if statement tests a single condition and performs an action if that condition is true and the else statement performs an action if the condition in the if statement is false, but what if there are more than two possibilities? Surely, any condition can be only true or false, but what if you needed to test a variable for more than one value? This is where the else if statement comes in. The else if statement is used in conjunction with the if statement. Unlike the else statement, it does not specifically perform a certain action if the condition in the if statement is false, but rather it performs an action if the condition in the if statement is another specific value specified in the else if statement itself.
Syntax:
if(condition is one value){
perform this action;
}
else if(condition equals another value){
perform this action;
}
Example:
<?php
$X = 7;
if($X == 5){
print "X is equal to 5";
}
else if($X == 7) {
print "X is equal to 7";
}
?>
In the above example, we are testing if the variable X is equal to 5, and if it is, print the message "X is equal to 5". We then use the else if statement to test if the variable X is equal to 7, and if it is, print the message "X is equal to 7".
The output of the above example:
Using if, else, and else if together
You can use the if, else, and else if statements together when you want to check a variable for a certain value many times. If it is not any of the checked values then the code specified by the else statement will be executed.
Syntax:
if(condition){
perform this action;
}
else if(condition equals another value){
perform this action;
}
else if(condition equals another value){
perform this action;
}
else if(condition equals another value){
perform this action;
}
else{
if the condition was not equal to any of the values tested in
the if statement and all the else if statements, then perform
this action;
}
Example:
<?php
$X = 5;
if($X == 9){
print "X is equal to 9";
}
else if($X == 7){
print "X is equal to 7";
}
else if($X == 3){
print "X is equal to 3";
}
else if($X == 15){
print "X is equal to 15";
}
else{
print "X is not equal to 9, 7, 3, or 15. X is equal
to " . $X;
}
?>
Output:
NOTE: When using if, else if, and else statements - if the action dictated by these statements contains more than one line of code, it should be sorrounded by curly braces, otherwise the curly braces are optional. Although, curly braces should always be used with conditional statements. Doing so is good convention.
Example:
<?php
$X = 5;
$Y = 7;
if($X == 9){
print "X is equal to 9";
print "Y is equal to 7";
}
else if($X == 7) print "X is equal to 7";
else if($X == 3){
print "X is equal to 3";
print "Y is equal to 7";
}
else if($X == 15){
print "X is equal to 15";
print "Y is equal to 7";
}
else print "X is not equal to 9, 7, 3, or 15,
it is equal to 5";
?>
In the above example, some of the conditional statements dictate actions with one line of code - not sorrounded by curly braces, and some conditional statements dictate actions with more than one line of code - sorrounded by curly braces.
The switch statement
The switch statement is specifically designed for comparing one variable to a number of possible values. It can be thought of as a substitute for the if, else if, else structure. There is an important keyword used within the switch statement, and that keyword is the break keyword. The break keyword is used to make sure that the switch statement will not fall through to the next possible value, even if that value is incorrect within the switch statement.
Syntax:
switch(variable){
case possible value:
perform this action;
break;
case possible value:
perform this action;
break;
case possible value:
perform this action;
break;
default:
perform this action if none of the values match;
}
Example:
<?php
$X = 7;
switch($X){
case 1:
print "X is equal to 1";
break;
case 2:
print "X is equal to 2";
break;
case 3:
print "X is equal to 3";
break;
case 7:
print "X is equal to 7";
break;
default:
print "X is not equal to
any of the values specified";
}
?>
In the above example, the variable X is initialized to 7, and is tested for a number of values. If X is equal to 1, the message "X is equal to 1" will be printed. If X is equal to 2, the message "X is equal to 2" will be printed. If X is equal to 3, the message "X is equal to 3" will be printed. If X is equal to 7, the message "X is equal to 7" will be printed. If X is equal to neither 1, 2, 3, or 7, the message "X is not equal to any of the values specified" will be printed.
Output of the above example:
The ternary operator
The ternary operator is the question mark symbol (?), it works the same way as the if-else structure.
Syntax:
variable = (condition) ? value the variable will take if condition is true: value the variable will take if condition is false
Example:
<?php
$x;
$y = 9;
$x = ($y==4) ? 5: 15;
print "x = " . $x;
?>
Output:




