[SOLVED] Setlocale() and sprintf() string representation (Paypal Gateway)
Posted: Thu Nov 12, 2009 5:45 pm
I have discovered a weird PHP problem described here: http://dev.cmsmadesimple.org/bug/view/4300
The bug applies to the module Paypal Gateway, but the problem is general:
This piece of code does format numbers to decimal even if according to the PHP documentation:
If you comment setlocale() then the string is correct:
1. Can somebody explain, why the argument %s of the sprintf() formats numbers and does not take them as string representation?
2. How to convert the variable $value in the given example to string? Assume that the values come from database or _REQUEST, so that the direct assignment $value = "2.4" is NOT possible.
----------------------------------------------------
The above code is tested on this environment:
Server OS: Linux 2.6.24-etchnhalf.1-686 On i686
Server Software: Apache/2.2.3 (Debian) mod_python/3.2.10 Python/2.4.4 mod_ssl/2.2.3 OpenSSL/0.9.8c
Php Information:
* phpversion: 5.2.0-8+etch13
* md5_function: On (True)
* gd_version: 2
* tempnam_function: On (True)
* magic_quotes_runtime: Off (False)
* memory_limit: 100000000
* max_execution_time: 30
* safe_mode: Off (False)
* session_save_path: /var/lib/php5 (1733)
* session.use_cookies: On (True)
Server Information:
* Server Api: cgi-fcgi
* Server Db Type: MySQL (mysql)
* Server Db Version: 5.0.32
The bug applies to the module Paypal Gateway, but the problem is general:
Code: Select all
<?php
setlocale (LC_ALL, 'de_DE.UTF8');
$key = "field";
$value = 2.4;
$fmt = 'key="%s" value="%s"';
echo sprintf($fmt,$key,$value);
?>
The output is:s - the argument is treated as and presented as a string.
Code: Select all
key="field" value="2,4"
Code: Select all
key="field" value="2.4"
2. How to convert the variable $value in the given example to string? Assume that the values come from database or _REQUEST, so that the direct assignment $value = "2.4" is NOT possible.
----------------------------------------------------
The above code is tested on this environment:
Server OS: Linux 2.6.24-etchnhalf.1-686 On i686
Server Software: Apache/2.2.3 (Debian) mod_python/3.2.10 Python/2.4.4 mod_ssl/2.2.3 OpenSSL/0.9.8c
Php Information:
* phpversion: 5.2.0-8+etch13
* md5_function: On (True)
* gd_version: 2
* tempnam_function: On (True)
* magic_quotes_runtime: Off (False)
* memory_limit: 100000000
* max_execution_time: 30
* safe_mode: Off (False)
* session_save_path: /var/lib/php5 (1733)
* session.use_cookies: On (True)
Server Information:
* Server Api: cgi-fcgi
* Server Db Type: MySQL (mysql)
* Server Db Version: 5.0.32