String Equals Zero In PHP

Due to the weakly-typed nature of PHP you can do some odd things, some of which are good, and some of which will enable you to shoot yourself in the foot. Take the following little snippet.

echo '1' + 5;

In some languages this might cause the program to fall over, but PHP will try to evaluate any string into an integer. In this case it converts the string to an integer 1 and adds this to 5 to make 6.

As an aside, if you did this in JavaScript then you would find the opposite result. Because the concatenation character is the same as the addition character JavaScript will always try to truncate the value if any of the present values are a string. So the result in JavaScript would be "15".

If we change the string to a string of "one" and then did the same then the result is 5.

echo 'one' + 5;

This is because if PHP can't translate the string to an integer it will assume that it is 0.

We can take this to another level by using comparison. Looking at the PHP type comparison tables you can see that there is a lot of different ways that two values can be compared.

Take a look at the following snippet. What would expect the outcome of this to be?

$a = 'a string';
$b = 0;
 
if ( $a == true && $b == false && $a == $b ) {
 exit;
}

Well the answer is that the program would exit because all of these comparisons equal true.

'a string' == true equates to true because PHP will evaluate any non empty string to true if compared with a boolean.

0 == false equates to true because the integer 0 is evaluated as false when compared with a boolean.

'a string' == 0 also evaluates to true because any string is converted into an integer when compared with an integer. If PHP can't properly convert the string then it is evaluated as 0. So 0 is equal to 0, which equates as true.

To get around this issue you can use the === operator in place of the == operator. This operator (also called triple-equals operator) will only evaluate to true not only if the two values have the same value, but also if they are the same type. So if we changed the example to use the triple-equals operator then all of the terms would evaluate to false. This is because a string cannot be a boolean, an integer cannot be a boolean and a string does not equal an integer.

The difference between the two equals operators is important to remember. Each has it's own use, but if you are in any doubt of the type of your values then use the triple-equals operator, especially when passing the test would spell disaster for your program.

The triple-equals operator is essential when using functions like strpos(). This is because it will return false when the string is not found. As in the following example where the $position variable equals false.

$position = strpos('abcd','z');

But what happens if the string is found at position 0? If you used the double-equals operator then you will find that your position will equate to false.

Comments

This is very useful to know. Thanks.
Permalink

useful, thanks!

Permalink

Thank you Philip.



I was looking for a satisfactory reason behind this. Found it here....

Permalink

Thank you Philip. It really helped.

Permalink

Add new comment

The content of this field is kept private and will not be shown publicly.
CAPTCHA
9 + 10 =
Solve this simple math problem and enter the result. E.g. for 1+3, enter 4.
This question is for testing whether or not you are a human visitor and to prevent automated spam submissions.