php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Bug #35215 Design Discussion: setlocale and type juggling
Submitted: 2005-11-14 15:51 UTC Modified: 2005-11-14 22:37 UTC
From: timo dot hummel at 4fb dot de Assigned:
Status: Not a bug Package: Strings related
PHP Version: 5.0.5 OS: any
Private report: No CVE-ID: None
View Add Comment Developer Edit
Anyone can comment on a bug. Have a simpler test case? Does it work for you on a different platform? Let us know!
Just going to say 'Me too!'? Don't clutter the database with that please !
Your email address:
MUST BE VALID
Solve the problem:
33 - 25 = ?
Subscribe to this entry?

 
 [2005-11-14 15:51 UTC] timo dot hummel at 4fb dot de
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>";
?>


Patches

Add a Patch

Pull Requests

Add a Pull Request

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2005-11-14 16:47 UTC] mike@php.net
Not a bug.

I don't like that behaviour either, anyway there's number_format() and *printf().

 [2005-11-14 18:47 UTC] timo dot hummel at 4fb dot de
I know it is not a bug, it's rather a design issue. However, I suggest to implement something to get the technical value of a float variable. This design issue renders either floating-point values or locale settings useless.

Something like http://www.php.net/strval which ignores the locale settings would be more useful:

$float = 0.12;
$sql = "SELECT * FROM test WHERE value=".strval($float, IGNORE_LOCALE);

It would be nice if something like that would be included in a later version of the zend engine or in PHP itself.
 [2005-11-14 20:01 UTC] mike@php.net
There's number_format() and *printf().

 [2005-11-14 22:37 UTC] derick@php.net
And this is intentional too.
 
PHP Copyright © 2001-2024 The PHP Group
All rights reserved.
Last updated: Thu Mar 28 08:01:28 2024 UTC