note 114523 added to language.types.string
| From: | necrodust44 at gmail dot com | Date: | Tue, 04 Mar 2014 01:52:30 +0000 |
| Subject: | note 114523 added to language.types.string | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-198774@lists.php.net to get a copy of this message | ||
String conversion to numbers.
Unfortunately, the documentation is not correct.
«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).»
It is not said and is not shown in examples throughout the documentation that, while converting
strings to numbers, leading space characters are ignored, like with the strtod function.
<?php
echo " \v\f \r 1234" + 1; // 1235
var_export ("\v\f \r 1234" == "1234"); // true
?>
However, PHP's behaviour differs even from the strtod's. The documentation says that if
the string contains a "e" or "E" character, it will be parsed as a float, and
suggests to see the manual for strtod for more information. The manual says
«A hexadecimal number consists of a "0x" or "0X" followed by a nonempty
sequence of hexadecimal digits possibly containing a radix character, optionally followed by a
binary exponent. A binary exponent consists of a 'P' or 'p', followed by an
optional plus or minus sign, followed by a nonempty sequence of decimal digits, and indicates
multiplication by a power of 2.»
But it seems that PHP does not recognise the exponent or the radix character.
<?php
echo "0xEp4" + 1; // 15
?>
strtod also uses the current locale to choose the radix character, but PHP ignores the locale, and
the radix character is always 2E. However, PHP uses the locale while converting numbers to strings.
With strtod, the current locale is also used to choose the space characters, I don't know about
PHP.
----
Server IP: 72.52.91.14
Probable Submitter: 72.52.91.14
----
Manual Page -- http://php.net/manual/en/language.types.string.php
Edit -- https://master.php.net/note/edit/114523
Del: integrated -- https://master.php.net/note/delete/114523/integrated
Del: useless -- https://master.php.net/note/delete/114523/useless
Del: bad code -- https://master.php.net/note/delete/114523/bad+code
Del: spam -- https://master.php.net/note/delete/114523/spam
Del: non-english -- https://master.php.net/note/delete/114523/non-english
Del: in docs -- https://master.php.net/note/delete/114523/in+docs
Del: other reasons-- https://master.php.net/note/delete/114523
Reject -- https://master.php.net/note/reject/114523
Search -- https://master.php.net/manage/user-notes.php