How to use Single, Double and Triple Equality sign in PHP

One of the errors that newbies php developers easily run into is 'Semantic error'.This is because unlike syntactic errors,semantic errors do not throw up error messages but the results of the operation might be different from the expected one.



 In this article, we will be sharing the practical differences between the single equality sign(=), double equality sign(==) and triple equality sign(===) in PHP. 

 1.The Single Equality sign(=) 

The Single equality sign, is used primarily by every programming language for assignment purpose.

 By Assignment purpose, We mean, assigning data to a variable ,or assigning a variable to a variable.

 Let us look at the practical examples below on how the single equality sign works in programing generally.
 Let us assume ,we want to assign integer value 4 to variable x.

 Your code will look like this; 
$x = 4;

 Another example of using a single equality sign is to assign a variable to a variable. Recall previously,our "x" is equals to 4 , and now we want to assign the value of x to y ,such that x will be equals to y. 

example below; 
$y = $x; 

the above examples means that variable y as the same value as variable x because ,x has been assigned to Y. So in conclusion, you use single equality sign (=) ,when you want to assign a value to a variable ,or variable to a variable.

 2. The Double Equality Sign(==) 

The double equality sign is used for comparison between two variables or entities. 

The double equality sign , should be used when you are trying to establish a condition of checking if two variables are equal. 

 For example ,let say the value of variable x is 6, while that of y is 8. To compare the two variables If they are the same, using a single equality sign will not give you what you expect, so in this cases , double equality sign is the right operator to use . 

 Let's look at the practical example,

  if($x ==$y){ echo "variables are the same";}

 else{ echo "Variable ain't the same";} 

 You can see from the example above, we are trying to compare variable x and y if they are the system using the if conditions and the double equality sign . The results here, will be "variable are not the same" because both variables contains different variables. so basically, we use double equality sign, to compare two variables . 

 3. Triple Equality Sign(===)

The triple equality sign, is used in advanced comparison of two variables. This goes beyond comparing the variable values, but also if they are of the same data type.

 Let us look at the illustration below; 

 $x = 3; // integer variable 

$y="3"; // string variable; 

block code 1.

 if($x ==$y){ echo "variables are the same";}

 else{ echo "Variable ain't the same";} 

block code 2

 if($x ===$y){ echo "variables are the same";}

 else{ echo "Variable ain't the same";}

 Results.

 block code 1 will output the results "variable are the same", because we used double equality sign which only compares the variable value and not the data type.

 Block code 2 will output, " Variable are not the same " this is because the triple equality sign (===) compares both the variable value and data type.

You can see that x is and integer variable while y is a string which makes them differ. So while comparing variables value and data types, use the triple equality sign. 

hope this information was useful. Do well to comment your opinion below.

Comments

Popular posts from this blog

"Chelsea are blessed"-Cesc Fabregas

How to Maintain Your Smart phone's Battery Energ level