So the difference between = , == and === is simple and clear. These all operators used in programming languages for different purposes. Means this is basic syntax for almost all programming languages, like Java, PHP, JavaScript, C#, C++ and many more. What does they mean actually?
It’s very important to know when and where to use single, double and triple equal operators. So let’s first of all describe the usage of each operator one by one to know. What is the difference between single equal, double equal and triple equal.
What does = means in programming languages.
= operator or single equal used to make left side equal to right side. Means if you declare something means a variable or constant on left side. The left side is the label of that variable or constant. And the right side to equal is the value the left side’s label holds.
In other words equal operator will make left side hold the value of right side. We can make left side equal to strings, equal to other variables, or results as well. You can even store true or false, 1 or 0, any string with double quotes, numbers and you can even store variable into variable using equal operator.
In example below i am going to show you how and what you can store using single equal operator.
//Makgin left side variable equal to a string.
$myName = 'Ateeq Rafeeq';
//Now left Side variable myName holds actually my Name.
//declaring two variables and making first equal to second.
$a = 'I am a';
$b = 'I am b';
$a = $b; //after this line $a variable holds string 'I am b' cause this was on left side.
//calculation result by equal sign.
$firstNum = 4;
$secondNum = 5;
$sum = $firstNum+$secondNum;
//Now $sum holds 5+4=9
So single equal or = sign makes left side equals to right side.
What does == means in programming languages.
In programming languages == sign or double equal sign means we are comparing right side with left side. And this comparison returns true or false. We usually use this comparison inside if condition to do something specific.
Double equal operator is a very common used operator after single equal. Its going to be used in various places in your code. Actually to make our code intelligent and able to make some decisions. We compare things with each other and then if else conditions work with these types of comparisons.
== sign examples
$myName = 'Ateeq Rafeeq';
$myFriend = 'Ateeq Rafeeq';
$myFather = 'Rafeeq';
$myNum = 4;
if($myName == $myFriend) {
echo 'You and Your friends are equal';
}//comparing myName and myFriend
if($myName == $myFather) {
echo 'This will not run cause above statement will return false.';
} else {
echo 'Your name and your father name is not equal.';
}
if($myNum = 4) {
echo '$myNum and 4 is equal.';
}
What does === triple equal means
Just like double equal operator === also used to compare two values on left and right. This will also return true or false based on comparison. Triple equal operator is also common used in if else conditions, while loops and some other places in code.
So the difference between == and === is simple. == Will not worry about variable type you are comparing. For example if you compare ‘1’ with 1 double equal will return true. But === will return false reason is types of both numbers are different. ‘1’ is a string and 1 is a number.
So you can say triple equal does not only see if both sides are equal but also cares if types are same and equal.
// Let's declare some values
$MyNumber = '123';
$myFriend = 123;
$mybrother = 123;
//Let's compare my number with my friend and this will return false, but in double equal this would be true return.
if($myNumber === $myFriend) {
echo "I am not going to work cause we are not equal for ===";
} else {
echo "Yes i work cause if condition returned false.";
}
// let's compare my friend with my brother both have same types of data so the return will be true that's it.
Difference between = , == and ===
Let’s say single equal = will make left side equal to right side. And that’s how equal works. The double equal or == will compare if both sides equal this is not strict comparison can ignore quotes and types of variables. The last === triple equal will make sure both sides are not only equal but also same types. Triple equal you can say make sure its identical to left side.
Triple equal mostly used while comparing numbers like for money or transactions. Cause the plus, minus and other mathematical operators cannot work well when number is stored as a string. For example multiplying 795 x ‘123’ will produce error cause ‘123’ is a string.
3 Comment(s)
Amazing my doubt is clear
Amazing my doubt is clear
Cool stuff