PHP if Operators
Comparison Operators
• If statements usually contain conditions that compare two values.
Example
<!DOCTYPE html> <html> <body> <?php $t = 14; if ($t == 14) { echo "Welcome to Codelines!"; } ?> </body> </html>
Output
Welcome to Codelines!
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 identica |
> | 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 |
• Logical Operators
Example
<!DOCTYPE html> <html> <body> <?php $a = 200; $b = 33; $c = 500; if ($a > $b && $a < $c ) { echo "Both conditions are true"; } ?> </body> </html>
Output
Both conditions are true
Here are the PHP logical operators to use in if statements:
Operator | Name | Result |
---|---|---|
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 |