Something that we came across this morning, and something to be wary of in the future, is how you check the parameter type of a $mixed variable in a method signature.
i.e. Is the parameter a string or a number?
If you’re not careful you can get unexpected results…
As we’ve discovered, using ctype functions can be dangerous as ctype_digit() for example expects a string as a parameter, so if you pass it an integer it will convert it to the ASCII equivalent, thus:
ctype_digit(65) = false
Because it’s basically doing:
ctype_digit('A') = false
And A isn’t a number. The exact opposite problem exists with ctype_alpha
ctype_alpha(65) = true
Because again it’s basically doing:
ctype_alpha('A') = true
One way to get around this, although of course you must be careful too, is to use is_numeric() to determine whether a variable is a number or not. Do not use is_string() or is_integer() as they have exactly the same problems as above, in that:
is_integer("65") = false is_string("65") = true
Whereas:
is_numeric("65") = true is_numeric(65) = true
However as has been mentioned to me since I wrote this, is that you have to be careful when handling floating point numbers, as if they end up being represented with a mantissa and exponent you can run into similar types of problems…