Tuesday, 6 April 2021

Php: Comparison operators

 

Comparison operators used to compare two different values and evaluate the result as either TRUE or FALSE.

 

Following table summarizes the comparison operators.

 

Operator

Name

Example

Description

==

Equal

$a == $b

Return true if $a is equal to $b after type juggling.

===

Identical

$a === $b

Return true if $a is equal to $b, and they are of the same type.

!=

Not equal

$a != $b

Return true if $a is not equal to $b after type juggling.

<> 

Not equal

$a <> $b

Return true if $a is not equal to $b after type juggling.

!==

Not identical

$a !== $b

Return true if $a is not equal to $b, or they are not of the same type.

Less than

$a < $b

Return true if $a is strictly less than $b.

<=

Less than or equal to

$a <= $b

Return true if $a is less than or equal to $b.

Greater than

$a > $b

Return true if $a is strictly greater than $b.

>=

Greater than or equal to

$a >= $b

Return true if $a is greater than or equal to $b.

 

comparison_operators_demo.php

#!/usr/bin/php

<?php
$a = 10;
$b = 11;
$c = '10';

echo "a : $a\n";
echo "b : $b\n";
echo "c : '$c'\n";

echo "\n" . '$a == $b : ';
var_dump($a == $b);

echo '$a == $c : ';
var_dump($a == $c);

echo '$a != $b : ';
var_dump($a != $b);

echo '$a != $c : ';
var_dump($a != $c);

echo '$a === $b : ';
var_dump($a === $b);

echo '$a === $c : ';
var_dump($a === $c);

echo '$a !== $b : ';
var_dump($a !== $b);

echo '$a !== $c : ';
var_dump($a !== $c);

echo '$a < $b : ';
var_dump($a < $b);

echo '$a > $b : ';
var_dump($a > $b);

echo '$a <= $b : ';
var_dump($a <= $b);

echo '$a >= $b : ';
var_dump($a >= $b);

echo '$a < $c : ';
var_dump($a < $c);

echo '$a > $c : ';
var_dump($a > $c);

echo '$a <= $c : ';
var_dump($a <= $c);

echo '$a >= $c : ';
var_dump($a >= $c);

?>

Output

#!/usr/bin/php

<?php
$a = 10;
$b = 11;
$c = '10';

echo "a : $a\n";
echo "b : $b\n";
echo "c : '$c'\n";

echo "\n" . '$a == $b : ';
var_dump($a == $b);

echo '$a == $c : ';
var_dump($a == $c);

echo '$a != $b : ';
var_dump($a != $b);

echo '$a != $c : ';
var_dump($a != $c);

echo '$a === $b : ';
var_dump($a === $b);

echo '$a === $c : ';
var_dump($a === $c);

echo '$a !== $b : ';
var_dump($a !== $b);

echo '$a !== $c : ';
var_dump($a !== $c);

echo '$a < $b : ';
var_dump($a < $b);

echo '$a > $b : ';
var_dump($a > $b);

echo '$a <= $b : ';
var_dump($a <= $b);

echo '$a >= $b : ';
var_dump($a >= $b);

echo '$a < $c : ';
var_dump($a < $c);

echo '$a > $c : ';
var_dump($a > $c);

echo '$a <= $c : ';
var_dump($a <= $c);

echo '$a >= $c : ';
var_dump($a >= $c);

?>


Note

If one operand is a number and the other one is a numeric string, then the comparison is done numerically.



 

 

 

Previous                                                    Next                                                    Home

No comments:

Post a Comment