PHP MYSQL - Operators - 5.3 Comparison Operators

PHP Comparison Operators

The PHP comparison operators are used to compare two values (number or string):

OperatorNameExampleResult
==Equal$x == $yReturns true if $x is equal to $y
===Identical$x === $yReturns true if $x is equal to $y, and they are of the same type
!=Not equal$x != $yReturns true if $x is not equal to $y
<>Not equal$x <> $yReturns true if $x is not equal to $y
!==Not identical$x !== $yReturns true if $x is not equal to $y, or they are not of the same type
>Greater than$x > $yReturns true if $x is greater than $y
<Less than$x < $yReturns true if $x is less than $y
>=Greater than or equal to$x >= $yReturns true if $x is greater than or equal to $y
<=Less than or equal to$x <= $yReturns true if $x is less than or equal to $y

Example ( ==)

<!DOCTYPE html>
<html>
<body>

<?php
$x = 10;  
$y = "10";

var_dump($x == $y); // returns true because values are equal
?>  

</body>
</html>

Output: 
bool(true)
because values are same, even datatypes are different.

Example (===)
<!DOCTYPE html>
<html>
<body>

<?php
$x = 10;  
$y = "10";

var_dump($x === $y); // returns false because types are not equal
?>  

</body>
</html>

Output: 
bool(false)
because values same but   datatypes are different.

Example (!=)

 <!DOCTYPE html>

<html>

<body>

<?php

$x = 100;  

$y = "100";

var_dump($x != $y); // returns false because values are equal

?>  

</body>

</html>

Output: 
bool(false)

Example (<>)

 <!DOCTYPE html>

<html>

<body>

<?php

$x = 100;  

$y = "100";

var_dump($x != $y); // returns false because values are equal

?>  

</body>

</html>

Output: 
bool(false)

Example(!==)

<!DOCTYPE html>
<html>
<body>

<?php
$x = 100;  
$y = "100";

var_dump($x !== $y); // returns true because types are not equal
?>  

</body>
</html>
Output:
bool(true)


Example (>)
<!DOCTYPE html>
<html>
<body>

<?php
$x = 100;
$y = 50;

var_dump($x > $y); // returns true because $x is greater than $y
?>  

</body>
</html>
Output:
bool(true)

Example (>=)
<!DOCTYPE html>
<html>
<body>

<?php
$x = 100;
$y = 50;

var_dump($x > =$y); // returns true because $x is greater than $y
?>  

</body>
</html>
Output:
bool(true)

Example (<)
<!DOCTYPE html>
<html>
<body>

<?php
$x = 100;
$y = 50;

var_dump($x < $y); // returns true because $x is greater than $y
?>  

</body>
</html>
Output:
bool(false)

Example (<=)
<!DOCTYPE html>
<html>
<body>

<?php
$x = 100;
$y = 50;

var_dump($x > $y); // returns true because $x is greater than $y
?>  

</body>
</html>
Output:
bool(false)


Popular posts from this blog

COPA Bits for Computer Based Test (CBT) by Venugopal Vanjarapu

COPA

Operating System