note 41318 added to function.setlocale

From: Date: Tue, 06 Apr 2004 13:37:39 +0000
Subject: note 41318 added to function.setlocale
Groups: php.notes 
Request: Send a blank email to php-notes+get-67705@lists.php.net to get a copy of this message
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 ---- Manual Page -- http://www.php.net/manual/en/function.setlocale.php Edit -- http://master.php.net/manage/user-notes.php?action=edit+41318 Delete -- http://master.php.net/manage/user-notes.php?action=delete+41318&report=yes Reject -- http://master.php.net/manage/user-notes.php?action=reject+41318&report=yes Search -- http://master.php.net/manage/user-notes.php

« previous php.notes (#67705) next »