Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

matthewinSpire's avatar

Error: A non well formed numeric value encountered

I am getting the above error, which references two things: helpers.php, which is a bunch of helper functions, and CartController.php. I have searched both files and associated functions with no luck.

From helpers.php:

function getNumbers()
{
$tax = config('cart.tax') / 100;
$discount = session()->get('coupon')['discount'] ?? 0;
$code = session()->get('coupon')['name'] ?? null;
$newSubtotal = (Cart::subtotal() - $discount);  //The error points to this line
if ($newSubtotal < 0) {
    $newSubtotal = 0;
}
$newTax = $newSubtotal * $tax;
$newTotal = $newSubtotal * (1 + $tax);

return collect([
    'tax' => $tax,
    'discount' => $discount,
    'code' => $code,
    'newSubtotal' => $newSubtotal,
    'newTax' => $newTax,
    'newTotal' => $newTotal,
]);
 }

From CartController.php:

/**
* Display a listing of the resource.
*
* @return \Illuminate\Http\Response
*/
public function index()
{
    $relatedProducts = Product::relatedProducts()->get();

    return view('cart/cart')->with([
        'relatedProducts' => $relatedProducts,
        'discount' => getNumbers()->get('discount'),  //The error points to this line
        'newSubtotal' => getNumbers()->get('newSubtotal'),
        'newTax' => getNumbers()->get('newTax'),
        'newTotal' => getNumbers()->get('newTotal'),
    ]);
 }

I am using Gloudemans. I have done a composer update, searched online, but have found no success or much information.

Any suggestions? Thank you!

0 likes
9 replies
burlresearch's avatar

I can't imagine how Cart::subtotal() as a static will possibly generate the number you're looking for? Inspect, to see if these are even things that can be "subtracted":

function getNumbers()
{
$tax = config('cart.tax') / 100;
$discount = session()->get('coupon')['discount'] ?? 0;
$code = session()->get('coupon')['name'] ?? null;

dd([Cart::subtotal(), $discount]);
1 like
matthewinSpire's avatar

That results in the following:

array:2 [▼ 0 => "5,999.00" 1 => 0 ]

Is the comma in the Cart::subtotal() value preventing the two from being subtracted?

burlresearch's avatar

Yes - you have a type-casting issue. String may by converted to float implicitly, but the ',' is not an allowed character in a potential string representation.

>>> is_numeric('123.2')
=> true
>>> is_numeric('1,123.2')
=> false

I'd likely have ::subtotal() return a numeric value - you may have to str_replace(',', '', Cart::subtotal()) otherwise, which is awkward, everywhere.

2 likes
matthewinSpire's avatar

The problem is that the Cart::subtotal() should be a numerical value because it's pulling the price (or at least it should be) directly from the database. In the database the price is a numeric value and somehow, somewhere, it's getting converted to a string.

Cronix's avatar

In the database the price is a numeric value and somehow, somewhere, it's getting converted to a string.

That has to do with your pdo driver extension that php is using (not laravel related). If you're using mysql, use the mysqlnd php extension instead of pdo_mysql.

You can also cast it to a int/float/whatever in your Cart::subtotal() method, or if it's a property, you can use the $casts array. https://laravel.com/docs/5.6/eloquent-mutators#attribute-casting

1 like
matthewinSpire's avatar

"That has to do with your pdo driver extension that php is using (not laravel related). If you're using mysql, use the mysqlnd php extension instead of pdo_mysql."

Is there a walkthrough for switching from one to the other? Forgive me for asking (I did do a Google search), but I'm lost. Thank you!

Cronix's avatar

You have to install mysqlnd if you haven't (it's a php extension) and enable it in your php.ini and disable pdo_mysql, then restart the web server.

It's hard to tell you how as it's different for different operating systems. Just google "YourOSName OSVersion php mysqlnd"

matthewinSpire's avatar
matthewinSpire
OP
Best Answer
Level 1

Seems to be fixed by the following:

In config/cart.php set the thousand_separator to an empty string or you might get a 'non well formed numeric value encountered' error. It conflicts with number_format.

So, I removed the , from the thousand_separator and voila, everything works just fine now.

Thanks for your help!

3 likes
deasytech's avatar

Had same issue I converted it like this below, I hope it helps:

$tax_config = config('cart.tax'); $tax = floatval(implode(explode(',',$tax_config)));

Please or to participate in this conversation.