Home Tutorial Php Phpbasics Tutorial PHP Comparison Operator

 
 

PHP Comparison Operator
Posted on: March 20, 2010 at 12:00 AM
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;

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

echo "Larger number is= ".$c;

Output:

Larger number is= 45

Related Tags for PHP Comparison Operator :


Ask Questions?

If you are facing any programming issue, such as compilation errors or not able to find the code you are looking for.

Ask your questions, our development team will try to give answers to your questions.