Articles

Tutorial Learn PHP operators

PHP Comparison operators Lesson 7 Tutorials PHP

7. Comparison operators in PHP

7. Comparison operators in PHP

In the previous examples we've used the double equal sign to check on the condition of a value. There are several operators which can be used within PHP to test conditions.

== checks the condition if the value is the same
!= checks the condition if the value is NOT the same
< checks to see if the first value is less than the second
> checks to see if the first value is greater than the second
<= checks to see if the first value is less than or equal to the second
>= checks to see if the first value is greater than or equal to the second.

PHP Code example

$a = 1;
if($a==1) {/// result is true}
if($a!=1) {/// result is false}
if($a<1) {/// result is false}
if($a>1) {/// result is false}
if($a<=1) {/// result is true}
if($a>=1) {/// result is true}

This conditions can be used in logical statements using if and switch statements.

To add more options in you condition statements you can use the AND && OR || to expand your logical statements. AND is the same as && and OR is the same as || when used in your logic.

$a = 1;
$b = 10;

if($a==1 AND $b>2) {/// result is true because both conditions}
if($a==1 || $b<2) {/// result is true because at least one condition is met}

More advanced operators are the triple equal sign and the double equals with the exclamation mark. These are used to test if a value is the same and if to determine a true or false for the type. If the type is the same for example a number gets compared to string value will produce a false return.