In accordance with the documentation until PHP7.x string conversion was affected by locale
A value can be converted to a string using the (string) $variavel
or the function strval()
. The string conversion is done automatically in the scope of an expression where a string is required. This happens when using the functions of echo
or print
, or when a variable is compared to a string
. The sections on Types and Type Juggling make the following clearer.
A int
or float
is converted into a string that represents the number textually (including the exponent part for floats
). Floating point numbers can be converted using exponential notation (4.1E+6).
In PHP 8, the decimal point character is always the "dot" (.
). Before PHP 8, the decimal point character is defined in locale
of the script (LC_NUMERIC
), which is precisely what happened in your case.
In summary before PHP8 was affected by locale
, to take the test I used the German_Germany.1252
:
<?php
setlocale(LC_NUMERIC, 'German_Germany.1252');
// Apenas obtem o locale atual, sem alterar
$locale = setlocale(LC_ALL, 0);
echo 10.14;
echo "\n";
var_dump($locale);
In PHP7
10,14
999999999,14
string(99) "LC_COLLATE=C;LC_CTYPE=Portuguese_Brazil.1252;LC_MONETARY=C;LC_NUMERIC=German_Germany.1252;LC_TIME=C"
Output on PHP8:
10.14
string(78) "LC_COLLATE=C;LC_CTYPE=C;LC_MONETARY=C;LC_NUMERIC=German_Germany.1252;LC_TIME=C"
Possible causes of current output being comma:
- You may be using a
auto_prepend_include
who are using setlocale()
- The
LC_NUMERIC
(or LC_ALL
) may have been configured in /etc/default/locale
- Or it may even have been a setup on
.bashrc
Check out these situations (Setenv Apache I could not test, maybe in Fast-CGI there is some configuration that affects the locale
, If you find any details please update the reply).
It’s probably because of locale, but I don’t remember where PHP gets it from (whether it’s from the operating system or php.ini). Anyway, if you want to control how the number is formatted, regardless of the configuration of the locale, use
number_format
: https://answall.com/a/492324/112052– hkotsubo
I think I’m wrong @hkotsubo, but maybe I’m on the outside anyway I don’t know such behavior in PHP, I say specifically in a simple mathematical operation this shouldn’t occur, so far as I know, the setlocale (or default_locale in php.ini) affects many things, but not the output of echo, in matter of numbers what it will affect is the
localeconv()
, for both LC_MONETARY and LC_NUMERIC– Guilherme Nascimento
Marcelo, can print the
print_r(localeconv());
in the same script and return the results to [Edit] the question?– Guilherme Nascimento
@Guilhermenascimento I made a test here (PHP 7.4.3/Ubuntu 20) and if I do
setlocale(LC_ALL, 'pt_BR.utf8')
, theecho
showcase137,88
. If you change to, for example,setlocale(LC_ALL, 'en_US.utf8')
, then comes to show137.88
. But as I said, I don’t know exactly where PHP gets these configs from, so it could have been a coincidence or some specific config of my machine, I don’t know. In online IDE’s no difference, and my guess is that they don’t have the pt_BR locale installed, but again, it’s just my guess...– hkotsubo