Comparison Operators in PHP
PHP Operators – Part 3
Forward: In this part of the series we look at equality, identical and relational operators in PHP.
By: Chrysanthus Date Published: 10 Aug 2012
Introduction
Note: If you cannot see the code or if you think anything is missing (broken link, image absent), just contact me at forchatrans@yahoo.com. That is, contact me for the slightest problem you have about what you are reading.
The Equal Operator
It is ==, typed as a double assignment operator. The equal operator returns true if the operands on either sides are equal, otherwise it returns false.
The Not Equal Operator
The Not Equal operator is the opposite of the Equal Operator. The Not Equal operator is, != . It returns true if the operands are not equal, otherwise it returns false. Let us look at some examples:
Try the following code:
<?php
$myVar = 25;
$hisVar = 30;
if ($myVar != $hisVar)
{
echo "The values of the two variables are not equal.";
}
?>
myVar is 25, hisVar is 30. The condition is read like this: If myVar is not equal to hisVar, then the if-block will be executed. Since the values of the variables are not equal, (myVar != myVar) returns true.
In the following code, the values of the two variables are equal, so the condition returns false and the if-block is not executed.
<?php
$myVar = 50;
$hisVar = 50;
if ($myVar != $hisVar)
{
echo "The values of the two variables are not equal.";
}
?>
<> is also an operator. It is synonymous to !=.
The identical operator is ===. It is similar to the equal to operator, but here, it is not only the two values that have to be equal, the types of the two values should also be equal. For example, if two numbers are equal, they should both be integers or both be floats. 5 which is an integer is not identical to 5.0 which is a float. However, 5 which is an integer is equal to 5.0 which is a float. I hope you notice the difference between the use of the word identical and equal to. Read and try the following code:
<?php
$myVar = 5;
$hisVar = 5.0;
if ($myVar === $hisVar)
{
echo "The values of the two variables are identical.";
}
else
{
echo "The values of the two variables are not identical.";
}
?>
The Not Identical Operator
The not identical operator is !==. It is the opposite of the identical operator. Here, if the two values are not equal or not of the same type, true is returned; if the two values are equal but not of the same type, true is returned; if the two values are not equal but of the same type, true is returned. Read and try the following:
<?php
$myVar = 5;
$hisVar = 5.0;
if ($myVar !== $hisVar)
{
echo "The values of the two variables are not identical.";
}
?>
The Greater Than operator is, > . It returns true if the left operand is greater than the right operand. In the following example, the left operand is greater than the right operand. So the if-block is executed:
<?php
$var1 = 60;
$var2 = 70;
if ($var2 > $var1)
{
echo "The value of $var2 is greater than the value of $var1.";
}
?>
Read and try the above code.
Greater Than Or Equal Operator
The Greater Than or Equal operator is, >= (it is the math greater than sign followed by the math equal sign). It returns true if the left operand is greater than or equal to the right operand.
The Less Than Operator
The Less Than Operator is < .It returns true if the left operand is less than the right operand.
The Less Than or Equal Operator
The Less than or Equal operator is, <= . It returns true if the left operand is less than or equal to the right operand.
The Ternary Operator
The ternary operator is ?: . It is the ? and : signs separated. The syntax of this operator is:
condition ? return this value if true : return this other value if false
This gives you a simple if-condition. If it is evaluated to true, the value after the ? sign is returned. If the condition is evaluated to false the value after the : sign is returned. The return value can be assigned to a new variable. The following code illustrates this:
<?php
$a = 7;
$b = 8;
$c = $b>$a ? 50 : 40;
echo $c;
?>
$a and $b are integers. The condition is if $b is greater than $a. If it is, 50 is returned, else 40 is return. The return value is assigned to the new variable, $c.
The ?: operator is a left-to-right operator.
That is it for equality, identical, relational and ternary operators. Let us take a break here. Rendezvous in the next part of the series.
Chrys
Related Links
Major in Website DesignWeb Development Course
HTML Course
CSS Course
ECMAScript Course
NEXT