Learn PHP-16 : Comparison Operators in PHP

Posted: November 8, 2010 in PHP
Tags:

PHP comparison operators allow you to compare two values against each other. The comparison is read from left to right by the PHP engine. If you compare an integer with a string, the string is converted to a number. If you compare two numerical strings, they are compared as integers. It returns a numeric boolean value of “1”(=TRUE) if the expression comparison is met.

The following table shows their usage and result:

Name Usage Result
Equal $var1 == $var2 TRUE if $var1 is equal to
$var2.
Identical $var1 === $var2 TRUE if $var1 is equal to
$var2, and they are of the same type.
Not Equal $var1 != $var2 TRUE if $var1 is not equal
to $var2.
Not Equal $var1 <>
$var2
TRUE if $var1 is not equal
to $var2.
Not Identical $var1 !== $var2 TRUE if $var1 is not equal
to $var2, or they are not the same type.
Less Than $var1 < $var2 TRUE if $var1 is strictly
less than $var2.
Greater Than $var1 > $var2 TRUE if $var1 is strictly
greater than $var2.
Less Than or Equal to $var1 <= $var2 TRUE if $var1 is less than
or equal to $var2.
Greater Than or Equal to $var1 >= $var2 TRUE if $var1 is greater
than or equal to $var2.

<?php $var1 = 5; $var2 = 3; // Is Greater Than Comparison echo $var1 > $var2; // should output a "1" meaning TRUE ?>
Example:

1

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s