Values
A string, a number and what PHP is willing to convert silently.
A value has a type: int, float, string, bool, array, object, null. A variable can change type from one assignment to the next. That is handy in a short script and dangerous in a service: a function "seemed to take a number" and got a string from a form.
The string "12" often becomes the number 12 in arithmetic. The string "12ab" behaves even more surprisingly in older comparisons. That is why input at the boundary is better parsed explicitly than left to conversion. (int)"08" gives 8. (int)"ab" gives 0, and that zero is indistinguishable from a real zero unless you checked the shape of the text.
null is a separate empty value. The empty string "" is not null. Zero is not null. An undefined variable in modern PHP with warnings is a defect, not a quiet null. Do not read a key without checking that it exists.
var_export and types help with debugging, but they are not printed to the client. The task asks for a specific line. A debug dump next to it breaks the comparison just like an extra word.
Practice
Where the garbage goes
What does (int)"ab" equal?
- 0
- a parse error
- null
- ab
The empty string
Are an empty string and null the same thing? Answer yes or no.
Add numbers
Write function sum($a, $b) that returns the sum of two numbers.