note 108326 added to function.setlocale
| From: | LudovicoGrossi | Date: | Tue, 17 Apr 2012 13:15:34 +0000 |
| Subject: | note 108326 added to function.setlocale | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-187207@lists.php.net to get a copy of this message | ||
Please note that LC_NUMERIC (or LC_ALL) will convert the decimal separator every time you put it in
a string, EVEN IF THE STRING IS A QUERY.
This is a problem for every locale that uses the comma as a decimal separator. Look at this example:
<?php
setlocale(LC_ALL, "it_IT"); // locale with comma for decimals
$decimal_number = 0.5;
$query = "SELECT * FROM Table WHERE decimal_field > ".(float)$decimal_number; //
casting to float is useless, since the conversion to a string is done after
?>
Then, the query fill fail because it will be converted to:
SELECT * FROM Table WHERE decimal_field > 0,5
(note the comma!)
I suggest to be sure that LC_NUMERIC is set to a standard locale, then use other formatting
functions to display formatted numbers when needed.
<?php
setlocale(LC_ALL, 'it_IT');
setlocale(LC_NUMERIC, 'en_US'); // let's overwrite the decimal separator
?>
----
Server IP: 69.147.83.197
Probable Submitter: 79.34.65.106
----
Manual Page -- http://www.php.net/manual/en/function.setlocale.php
Edit -- https://master.php.net/note/edit/108326
Del: integrated -- https://master.php.net/note/delete/108326/integrated
Del: useless -- https://master.php.net/note/delete/108326/useless
Del: bad code -- https://master.php.net/note/delete/108326/bad+code
Del: spam -- https://master.php.net/note/delete/108326/spam
Del: non-english -- https://master.php.net/note/delete/108326/non-english
Del: in docs -- https://master.php.net/note/delete/108326/in+docs
Del: other reasons-- https://master.php.net/note/delete/108326
Reject -- https://master.php.net/note/reject/108326
Search -- https://master.php.net/manage/user-notes.php