Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

PHP - Random round behavior issue

I am actually facing a big problem with several websites (actually 3) based on Prestashop. The problem is that PHP or Prestashop randomly rounds prices to the nearest integer and it is not systematic.

Most of the time, it works perfectly, as it should, but sometimes (it can takes weeks or months between two problems), a price is rounded. My round mode option is correctly set to display two decimals.

The problem can occur when editing a product price in the back-office or when the customer is at the checkout step.

I tried to reproduce the problem, so I created a basic test: I retrieve a cart information and I display its price. I refreshed the page many times and I saw the price rounded a few times only. The most intriguous thing is that neither the context nor the code have changed between the beginning and the ending of the test.

I searched for help on Google and no one seemed to have this problem...

Did someone encounter this problem? Do you think it is a PHP issue or a Prestashop one? Thanks in advance for your help.

Here is the code of the round function Prestashop is using:

round($value, 2, PHP_ROUND_HALF_UP);

For information, the version of PHP 5.4.39.

like image 404
Sebj Avatar asked May 20 '15 14:05

Sebj


1 Answers

More than 2 years after, we figured out the issue. It was due to php5-fpm who does not handle locales per thread but per process. It is really clear in PHP documentation:

Warning The locale information is maintained per process, not per thread. If you are running PHP on a multithreaded server API like IIS, HHVM or Apache on Windows, you may experience sudden changes in locale settings while a script is running, though the script itself never called setlocale(). This happens due to other scripts running in different threads of the same process at the same time, changing the process-wide locale using setlocale().

Because the decimal separator has changed, PHP did not recognize decimals and truncated my numbers.

like image 77
Sebj Avatar answered Oct 17 '22 03:10

Sebj