1

I have been building an application with Laravel 5.0 and locally (within homestead) everything works as expected.

However, when I deploy the project a number of issues are apparent. One of which involves floats.

Homestead is PHP 5.6 whilst my server is 5.5.9. When I view a price on my local system it appears correctly as 9.99. However, on my server it does not display correctly but instead as 9.99000.

My schema has price fields set to float(12,5), hence the additional right padding, this is also required.

My question is simply what is the difference between PHP 5.5 and 5.6 (or even PDO/Eloquent) that would cause a floating point issue?

13
  • What's the code that is responsible for outputting those values? Commented Apr 13, 2016 at 22:15
  • It's standard eloquent. If I {{ $product->price}} php 5.5 will display 9.99000 where as php 5.6 will display 9.99. Commented Apr 13, 2016 at 22:18
  • Is the RDBMS version different as well from dev to production? Commented Apr 13, 2016 at 22:19
  • @Bogdan Both servers use Mysql 5.7.9 Commented Apr 13, 2016 at 22:22
  • The logical point of origin would be PDO since that's the layer responsible for processing that particular piece of information (since Eloquent is database agnostic and MySQL versions are the same, from a logical standpoint the issue should be on the PHP side). I'll take a look tomorrow and see if I can reproduce it (unless someone else comes with an explanation until then). Commented Apr 13, 2016 at 22:31

1 Answer 1

2

There's no difference between the PHP versions that would cause this. There are differences between different mysql drivers, however, and that could cause the issue that you are seeing.

Homestead comes with php5-mysqlnd installed, which is the "MySql Native Driver". When using this driver, floating points and integers fetched from the database will be assigned as numeric datatypes in PHP. If you are not using the native driver (php5-mysql), floating points and integers fetched from the database will be assigned as strings in PHP.

The following code demonstrates how this affects your output:

$f = 9.99000;
$s = "9.99000";

echo $f; // shows 9.99
echo $s; // shows 9.99000

You can check to see if the server is using the native driver with the command php -i | grep mysqlnd. This is just searching through your phpinfo() for any mention of the native driver. If this doesn't return anything, then you are not using the native driver, and your numeric data will be returned as strings.

If you do not have the native driver installed, you will need to remove the old driver and install the new driver:

apt-get remove php5-mysql

apt-get install php5-mysqlnd

Assuming this was your issue in the first place, this will fix it. You can also check out this question and answer for more information.

Sign up to request clarification or add additional context in comments.

1 Comment

Wow, thanks. I've learnt something new here and thanks for the link I wish I could've found that.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.