0

I have this error appear on my script

Warning: A non-numeric value encountered

the line is :

$new_shipping_weight += $shipping_weight;

my code

if ($shipping_weight <= 0 || is_null($shipping_weight)) $shipping_weight = 0.1;

var_dump(is_numeric($shipping_weight));

$new_shipping_weight += $shipping_weight;

var_dump(is_numeric($new_shipping_weight));

result :

bool(true) 


Warning: A non-numeric value encountered in 


bool(true)

Where is the problem ?

Thank you.

Naor Tedgi
  • 5,204
  • 3
  • 21
  • 48
Joe
  • 27
  • 1
  • 1
  • 8

2 Answers2

0

The assignment $new_shipping_weight += $shipping_weight expands to

$new_shipping_weight = $new_shipping_weight + $shipping_weight;

Thus both operators have to be non-numeric. Most likely $new_shipping_weight is not numeric before the assignment. It will be considered as 0, but the warning issued to make you aware.

Try this code and you will get the same result, with $a being non-numeric before and numeric after assignment. You will also see the warning:

$a = '';
$b = 1;
var_dump(is_numeric($a));
$a += $b;
var_dump(is_numeric($a));
hlg
  • 1,321
  • 13
  • 29
-2
$new_shipping_weight += is_numeric($shipping_weight);

try this one or $new_shipping_weight = is_numeric($shipping_weight) + is_numeric($new_shipping_weight);

  • How can that be the correct solution? `is_numeric()` will return a boolean value, thus adding either 0 or 1 to `$new_shipping_weight` and not the actual value of `$shipping_weight`! – arueckauer Feb 10 '19 at 22:10
  • No! `set_type()` also returns a boolean value. As Joe outline in his question, `$shipping_weight` is already a numeric value. Therefor type change/cast should not be necessary. – arueckauer Feb 10 '19 at 22:26
  • u right sorry and right code : `$new_shipping_weight += (double) $shipping_weight;` – Кирилл Лапко Feb 10 '19 at 22:31