Share on Google+Share on Google+

PHP Comparison Operator

Advertisement
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

Advertisements

Posted on: March 20, 2010 If you enjoyed this post then why not add us on Google+? Add us to your Circles

Share this Tutorial Follow us on Twitter, or add us on Facebook or Google Plus to keep you updated with the recent trends of Java and other open source platforms.