Reputation: 35734
What are the conversion rules for strings to ints in PHP?
For example I have the following that return 0:
var_dump((int)"test0");
var_dump((int)"test1");
var_dump((int)"test2");
and it appears that if the string starts with a digit then it will trim the characters and use the digit part only. As follows:
var_dump((int)"1test");
var_dump((int)"2test");
var_dump((int)"3test");
These return 1, 2 and 3 respectively.
Now, is this a definite rule, or are there exceptions to this?
Upvotes: 0
Views: 89
Reputation:
This is explained in PHP's manual:
http://php.net/manual/en/language.types.string.php#language.types.string.conversion
An excerpt:
The value is given by the initial portion of the string. If the string starts with valid numeric data, this will be the value used. Otherwise, the value will be 0 (zero). Valid numeric data is an optional sign, followed by one or more digits (optionally containing a decimal point), followed by an optional exponent. The exponent is an 'e' or 'E' followed by one or more digits.
Upvotes: 5