Thursday, March 14, 2013

Understanding Operators in PHP - Part 4

In our PHP Developer Journey tutorials, let's continue with our Understanding Operators part 4. If you are unaware of the initial parts then please go through the below links.

Assignment operators, Arithmetic operators, Array operators - Part 1

String operators, Logical Operators - Part 2

Bitwise Operators - Part 3

Comparison Operators:

Comparison operators, as the name itself says that, they will allow you to compare two values. Simply the comparison operators are used to determine the relationship between two variables.

Let's see the different types of comparison operators:

1. == (Equal): The == operator determines if two values are equal or not.

Demo of Equal (==) Operator:
$x = 46; // integer variable
$y = "46"; // string variable
var_dump($x == $y); // output: TRUE

Explanation: From the above code, we have one integer variable and another is string variable, So  the PHP interpreter will first convert the string variable to integer variable then perform comparison. Because the integer will have the higher priority than string. So the conversion takes place before the comparison. Since the two variables hold the same values but are of different type. 

2. != (Not Equal): The != operator determines if two values are different.

Demo of Not Equal (!=) Operator:
$x = 46; // integer variable
$y = "46"; // string variable
var_dump($x != $y); // output: FALSE

Explanation: From the above code, we have one integer variable and another is string variable, Since the two variables hold the same values but are of different type. Even though they are of different type the PHP interpreter returns FALSE.

3. < (Less than): The < operator determines whether the left operand’s value is less than the right operand’s.

Demo of Less than (<) Operator:
$x = 46; //integer
$y = 100; //integer
var_dump($x<$y); //output: TRUE

Explanation: From the above code, we have two integer variables, Since the two variables hold the different values but are of same type. Based on the comparison operator the PHP interpreter returns TRUE. Since 46 is less than 100.

4. > (Greater than): The > operator determines whether the left operand’s value is greater than the right operand’s.

Demo of Greater than (>) Operator:
$x = 46; //integer
$y = 100; //integer
var_dump($x>$y); //output: FALSE

Explanation: From the above code, we have two integer variables, Since the two variables hold the different values but are of same type. Based on the comparison operator the PHP interpreter returns FALSE. Since 46 is less than 100.

5. <= (Less than or Equal): The <= operator determines whether the left operand’s value is less than or equal to the right operand’s.

Demo of Less than or Equal (<=) Operator:
$x = 46; //integer
$y = 100; //integer
var_dump($x<=$y); //output: TRUE

Explanation: From the above code, we have two integer variables, Since the two variables hold the different values but are of same type. Based on the comparison operator the PHP interpreter returns TRUE. Since 46 is less than or equal 100.

6. >= (Greater than or Equal): The >= operator determines whether the left operand’s value is greater than the right operand’s.

Demo of Greater than or Equal (>=) Operator:
$x = 46; //integer
$y = 100; //integer
var_dump($x>=$y); //output: FALSE

Explanation: From the above code, we have two integer variables, Since the two variables hold the different values but are of same type. Based on the comparison operator the PHP interpreter returns FALSE. Since 46 is neither greater than nor equal to 100.

7. === (Identical): The === operator determines whether the value and the type of the two operands is the same.

Demo of Identical (===) Operator:

$x = 46;
$y = "46";
var_dump($x===$y);

Explanation: From the above code, we have one integer variable and another is string variable, Since the two variables hold the same values but are of different type. The above php code returns FALSE as strict equal operator will compare both value and type of $x and $y.

8. !== (Not Identical): The !== operator determines whether either the value or the type of the two operands is different.


Demo of Not Identical (!==) Operator:

$x = 46;
$y = "46";
var_dump($x!==$y);

Explanation: From the above code, we have one integer variable and another is string variable, Since the two variables hold the same values but are of different type. The above php code returns true though their values are equal but type of $x and $y are not equal (first one is integer type and second one is character type).

Note: All the comparison operators returns the Boolean value as a result.

While performing comparisons with the variables, the PHP interpreter first convert them if they are not of the same type. If the variables have the same type then no conversions will take place  These conversions will take place automatically by the PHP interpreter. Keep a note that, these conversions will be done based on the priority of the "Data Type". Means whatever the data type has the highest priority it will be taken first and so on. The Data Type here either integer, floating point, string, character like that.

Thanks for reading. Keep visiting for more stuff.

Did You Enjoy this Article ?

If yes, Then enter your email below to get

more such great articles in your inbox

For FREE !

No comments:

Post a Comment

Thank you for reading and commenting...