note 41318 deleted from function.setlocale by derick
| From: | derick@php.net | Date: | Tue, 30 Aug 2005 11:23:08 +0000 |
| Subject: | note 41318 deleted from function.setlocale by derick | ||
| References: | 1 | Groups: | php.notes |
| Request: | Send a blank email to php-notes+get-94411@lists.php.net to get a copy of this message | ||
Note Submitter: MagicalTux at FF.st
----
Be careful when using LC_ALL or LC_NUMERIC ! For example if you want to pass a numeric value
(example : 18.4) in a GET/POST form, you'll probably use :
<input type="text" name="numeric"
value="<?=floatval($num);?>" />
If you use locale fr_FR at this time, $_GET['numeric'] will have value string(18,4) ... if
you convert it back to float without re-doing a setlocale, PHP will truncate the value and
you'll get only 18 ...
And the main problem can be if you do cross-sites data transfert. Example : I have a website that
uses locale from the user settings. If the user have a French locale, I'll get something like
18,4 . If I pass that to paypal (pratical example) it will not work. So the best is to always set
LC_NUMERIC to "C" (posix).
Little example code :
<?php
$val=18.4; // any float value
echo 'Initial: '.$val."\n";
setlocale(LC_NUMERIC,'fr_FR');
$val=strval($val); // convert $val to string
echo 'French: '.$val."\n";
setlocale(LC_NUMERIC,'C'); // posix - note that $val is still a string !!!
$val=floatval($val); // convert it back to float
echo 'Bad convertion: '.$val."\n"; // !
?>
Output :
Initial: 18.4
French: 18,4
Bad convertion: 18