Twice now this week, which is impressive as it is only Tuesday, questions about variable types have popped up on IRC chat about how strings get evaluated and converted. PHP has the soft variable type system that allows you to do things like compare strings to integers, or take a variable that used to be an integer and store a string in it instead. This is unlike the strict typing of languages like C where when you create an integer variable, you better not be trying to store anything except an integer in it.
To hopefully clarify things for more people and also create a link I can send instead of re-explaining it every time, lets take a look at comparing and converting strings.
Comparing strings to other things
PHP also has two different “is equals” operators, “==” and “===”. The first checks if one value is equal to another, and the second checks if one value is equal and the same datatype. So for example, if we have two variables:
PHP Code:<?php
$int1 = 1;
$str1 = "1";
?>
Then these two statements will have different results:
PHP Code:<?php
if($int1 == $str1) echo "yes";
else echo "no";
if($int1 === $str1) echo "yes";
else echo "no";
?>
The output would be “yes” followed by “no”. This is because the soft typing allows 1 to be equal to “1″ while the strict typing will not.
Converting strings
PHP’s method for converting strings makes a lot of sense if you look at it from the string angle, but from the resultant angle it can be misleading. Here is why: strings do not technically “convert”, they “evaluate”.
When you convert a string to an integer and the string is not a number, the result is 0. When you convert a string to a boolean, an empty string will be false, but a non-empty string will be true. So take the following examples:
PHP Code:<?php
$test = "bob is so win";
var_dump($test);
// string(13) "bob is so win"
var_dump((int)$test);
// int(0) -- this was converted to an integer that is 0
var_dump((bool)$test);
// bool(true) -- evaluated to boolean true.
?>
From these examples, you can almost attempt to claim proof that true is equal to 0, but you know that is wrong as in programming false and 0 are mostly interchangeable. This is what makes things like if(!$name) { die('come on, enter your name already'); } work if $name had been empty. However, when converting a string to an integer, PHP will evaluate the string until a non-number value is encountered.
PHP Code:<?php
$test = "10 years ago";
var_dump((int)$test);
// int(10)
$test = "but 10 years ago";
var_dump((int)$test);
// int(0)
?>
In this case, the string “10 years ago” when converted to an integer results in the value of 10. PHP truncates the string at the first non-number character. That is why both “bob is win” and “but 10 years ago” ended up at 0, but “10 years ago” was converted to 10. If the string does not start with a number, it will always convert to 0. All three though, “bob is win”, “but 10 years ago”, and “10 years ago” will all evaluate to true if you typecast it to boolean.
tl;dr
- == compares values irregardless of type.
- === compares values and enforces they are even the same type.
- Strings converted to boolean will be false if they are empty, true if they are not.
- Strings converted to integers that have non-number characters in them will be truncated at the first non-number. If the string does not start with a number character, it will always be 0.
