PHP Comparison Operator


 

PHP Comparison Operator

In this tutorial we will study about comparison operator, comparison operator is used to compare two or more operands. Examples in this tutorial will make it more clear.

In this tutorial we will study about comparison operator, comparison operator is used to compare two or more operands. Examples in this tutorial will make it more clear.

Comparison Operator:

It is another kind of operator supported by PHP, with this operator you can compare two variables, whether the variables are equal or not, which variable is larger, which one is smaller etc.

PHP provides a special type of operator (= = =) which compares two variables and their data type .e.g. if we compare "23" (string data type) and 23 (integer data type) with = = operator, the result would be true, but (= = =) will display false because of the dissimilarity of data type.

Another way of comparison could be done by ternary operator (? :), in this operator we place the condition section before (?) and the true section after (?) and the false portion after (:).

Example:

<?

$a=12;

$b=12;

var_dump ($a==$b);echo "<br/>";

var_dump ($a===$b);echo "<br/>";

var_dump ($a!=$b);echo "<br/>";

var_dump ($a<>$b);echo "<br/>";

var_dump ($a<$b);echo "<br/>";

var_dump ($a>$b);echo "<br/>";

var_dump ($a<=$b);echo "<br/>";

var_dump ($a>=$b);echo "<br/>";

?>

OOutput:

bool(true)
bool(true)
bool(false)
bool(false)
bool(false)
bool(false)
bool(true)
bool(true)

Example:

<?php

var_dump("23"==23);

echo "<br/>";

var_dump("23"===23);

Output:

bool(true)
bool(false)

Example:

<?php

$a=23;

$b=45;

0

$c=($a>$b)?$a:$b;

echo "Larger number is= ".$c;

Output:

1

Larger number is= 45

Ads