If statements typically include conditions that compare two values.
Verify if $t is equal to 14:
$t if |
To compare two values, we use comparison operators.
Here are the PHP comparison operators used in if statements:
Operator |
Name |
Result |
== |
Equal |
Returns true if the values are identical |
=== |
Identical |
Returns true if the values and their data types are identical |
!= |
Not equal |
Returns true if the values are different |
<> |
Not equal |
Returns true if the values are unequal |
!== |
Not identical |
Returns true if the values or data types differ |
> |
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 evaluate multiple conditions, we can use logical operators, such as the && operator:
Check if $a is greater than $b and if $a is less than $c.
$a if |
Here are the PHP logical operators that can be used 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 multiple conditions within a single if statement.
Check if $a is one of the values: 2, 3, 4, 5, 6, or 7.
$a if |