PHP - The if Statement
The if statement executes some code if one condition is true.
Syntax
if (condition) {
// code to be executed if condition is true;
}
Example:
if (5 > 3) {
echo "Have a good day!";
}
Output :
"Have a good day!" if 5 is larger than 3:
We can also use variables in the if statement:
Example:
$t = 14;
if ($t < 20) {
echo "Have a good day!";
}
Output:
Have a good day!
Here are the PHP comparison operators to use in if statements:
Operator	Name	Result	
==	        Equal	                        Returns true if the values are equal	
===	        Identical                     	Returns true if the values and data types are identical	
!=	        Not equal	                        Returns true if the values are not equal	
<>	        Not equal	                        Returns true if the values are not equal	
!==  	Not identical	                Returns true if the values or data types are not identical	
>	        Greater than              	Returns true if the first value is greater than the second value	
<	        Less than	                        Returns true if the first value is less than the second value	
>=	        Greater than or equal to	Returns true if the first value is greater than, or equal to, the second value	
<=	        Less than or equal to	        Returns true if the first value is less than, or equal to, the second value	
To check more than one condition, we can use logical operators, like the && operator:
Example:
Check if $a is greater than $b, AND if $a is less than $c:
$a = 200;
$b = 33;
$c = 500;
if ($a > $b && $c < $a ) {
  echo "Both conditions are true";
}
Here are the PHP logical operators to use in if statements:
Operator	Name	Description	
and	        And	True if both conditions are true	
&&	        And	True if both conditions are true	
or	        Or	True if either condition is true	
||	        Or	True if either condition is true	
xor	        Xor	True if either condition is true, but not both	
!	        Not	True if condition is not true	
We can compare as many conditions as we like in one if statement:
Check if $a is either 2, 3, 4, 5, 6, or 7:
$a = 5;
if ($a == 2 || $a == 3 || $a == 4 || $a == 5 || $a == 6 || $a == 7) {
  echo "$a is a number between 2 and 7";
}
