From:             timo dot hummel at 4fb dot de
Operating system: any
PHP version:      5.0.5
PHP Bug Type:     Strings related
Bug description:  Design Discussion: setlocale and type juggling

Description:
------------
Currently, the whole way PHP does type juggling is affected using
setlocale. 

Right now, PHP respects the setlocale-Settings for LC_NUMERIC when doing
type-juggling (e.g. converting a float to a string or when outputting
data):

$float = 0.12;
setlocale(LC_ALL, "de_DE");
echo $float;

The simple example above outputs 0,12. If you need to build up SQL
queries, this behaviour is unwanted and causes errors:

$float = 0.12;
setlocale(LC_ALL, "de_DE");
$sql = "SELECT * FROM test WHERE value=$float;";

The query will be "juggled" to SELECT * FROM test WHERE value=0,12; -
which is not the expected result. 

If setlocale is used, there's no way (at least not to my knowledge) to
access the original, english (I also call it technical) representation of
a floating point number. 

This is not really a bug, but rather a limitation by design, but it
effectively prevents PHP developers from implementing multi-lingual
applications.



Reproduce code:
---------------
<?php
/* Nicer output for browsers */
echo "<pre>";

/* Predefine some variables to play around with */
$float = 0.12;
$sql = "SELECT * FROM test WHERE amount=";

setlocale(LC_ALL, "C");         // Set standard locale
echo strval($float). "\n";      // Outputs 0.12, which is OK
echo $sql . $float."\n";    // Outputs SELECT * FROM test WHERE
amount=0.12, which can be used by a database.


setlocale(LC_ALL, "de_DE");     // Set german locale
echo strval($float). "\n";      // Outputs 0,12, which is OK for display
purposes, but not for data processing
echo $sql . $float."\n";    // Outputs SELECT * FROM test WHERE
amount=0,12, which cannot be used by a database with english locale.

echo "</pre>";
?>


-- 
Edit bug report at http://bugs.php.net/?id=35215&edit=1
-- 
Try a CVS snapshot (php4):   http://bugs.php.net/fix.php?id=35215&r=trysnapshot4
Try a CVS snapshot (php5.0): 
http://bugs.php.net/fix.php?id=35215&r=trysnapshot50
Try a CVS snapshot (php5.1): 
http://bugs.php.net/fix.php?id=35215&r=trysnapshot51
Fixed in CVS:                http://bugs.php.net/fix.php?id=35215&r=fixedcvs
Fixed in release:            http://bugs.php.net/fix.php?id=35215&r=alreadyfixed
Need backtrace:              http://bugs.php.net/fix.php?id=35215&r=needtrace
Need Reproduce Script:       http://bugs.php.net/fix.php?id=35215&r=needscript
Try newer version:           http://bugs.php.net/fix.php?id=35215&r=oldversion
Not developer issue:         http://bugs.php.net/fix.php?id=35215&r=support
Expected behavior:           http://bugs.php.net/fix.php?id=35215&r=notwrong
Not enough info:             
http://bugs.php.net/fix.php?id=35215&r=notenoughinfo
Submitted twice:             
http://bugs.php.net/fix.php?id=35215&r=submittedtwice
register_globals:            http://bugs.php.net/fix.php?id=35215&r=globals
PHP 3 support discontinued:  http://bugs.php.net/fix.php?id=35215&r=php3
Daylight Savings:            http://bugs.php.net/fix.php?id=35215&r=dst
IIS Stability:               http://bugs.php.net/fix.php?id=35215&r=isapi
Install GNU Sed:             http://bugs.php.net/fix.php?id=35215&r=gnused
Floating point limitations:  http://bugs.php.net/fix.php?id=35215&r=float
No Zend Extensions:          http://bugs.php.net/fix.php?id=35215&r=nozend
MySQL Configuration Error:   http://bugs.php.net/fix.php?id=35215&r=mysqlcfg

Reply via email to