note 58824 added to language.types.float
| From: | Luzian at osu1 dot php dot net | Date: | Thu, 17 Nov 2005 08:03:34 +0000 |
| Subject: | note 58824 added to language.types.float | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-98685@lists.php.net to get a copy of this message | ||
Be careful when using float values in strings that are used as code later, for example when
generating JavaScript code or SQL statements. The float is actually formatted according to the
browser's locale setting, which means that "0.23" will result in "0,23".
Imagine something like this:
$x = 0.23;
$js = "var foo = doBar($x);";
print $js;
This would result in a different result for users with some locales. On most systems, this would
print:
var foo = doBar(0.23);
but when for example a user from Germany arrives, it would be different:
var foo = doBar(0,23);
which is obviously a different call to the function. JavaScript won't state an error,
additional arguments are discarded without notice, but the function doBar(a) would get 0 as
parameter. Similar problems could arise anywhere else (SQL, any string used as code somewhere else).
The problem persists, if you use the "." operator instead of evaluating the variable in
the string.
So if you REALLY need to be sure to have the string correctly formatted, use number_format() to do
it!
----
Manual Page -- http://www.php.net/manual/en/language.types.float.php
Edit -- http://master.php.net/manage/user-notes.php?action=edit+58824
Delete: added to the manual -- http://master.php.net/manage/user-notes.php?action=delete+58824&report=yes&reason=added+to+the+manual
Delete: bad code -- http://master.php.net/manage/user-notes.php?action=delete+58824&report=yes&reason=bad+code
Delete: spam -- http://master.php.net/manage/user-notes.php?action=delete+58824&report=yes&reason=spam
Delete: useless -- http://master.php.net/manage/user-notes.php?action=delete+58824&report=yes&reason=useless
Delete: non-english -- http://master.php.net/manage/user-notes.php?action=delete+58824&report=yes&reason=non-english
Delete: already in docs -- http://master.php.net/manage/user-notes.php?action=delete+58824&report=yes&reason=already+in+docs
Delete: other reasons -- http://master.php.net/manage/user-notes.php?action=delete+58824&report=yes
Reject -- http://master.php.net/manage/user-notes.php?action=reject+58824&report=yes
Search -- http://master.php.net/manage/user-notes.php