The equals comparison operator (==, two equal signs) enables you to test whether two values are equal. For example, you might use the expression
to test whether the values stored in $a and $b are the same.The result returned by this expression is true if they are equal or false if they are not.
You might easily confuse == with =, the assignment operator. Using the wrong operator will work without giving an error but generally will not give you the result you wanted. In general, nonzero values evaluate to true and zero values to false. Say that you have initialized two variables as follows:
If you then test $a = $b, the result will be true.Why? The value of $a = $b is the value assigned to the left side, which in this case is 7. Because 7 is a nonzero value, the expression evaluates to true. If you intended to test $a == $b, which evaluates to false, you have introduced a logic error in your code that can be extremely difficult to
find. Always check your use of these two operators and check that you have used the one you intended to use.
Using the assignment operator rather than the equals comparison operator is an easy mistake to make, and you will probably make it many times in your programming career.
$a == $b
You might easily confuse == with =, the assignment operator. Using the wrong operator will work without giving an error but generally will not give you the result you wanted. In general, nonzero values evaluate to true and zero values to false. Say that you have initialized two variables as follows:
$a = 5;
$b = 7;
$b = 7;
find. Always check your use of these two operators and check that you have used the one you intended to use.
Using the assignment operator rather than the equals comparison operator is an easy mistake to make, and you will probably make it many times in your programming career.
Comment