Warning: a Non-Numeric Value Encountered

Warning: A non-numeric value encountered

It seems that in PHP 7.1, a Warning will be emitted if a non-numeric value is encountered. See this link.

Here is the relevant portion that pertains to the Warning notice you are getting:

New E_WARNING and E_NOTICE errors have been introduced when invalid
strings are coerced using operators expecting numbers or their
assignment equivalents. An E_NOTICE is emitted when the string begins
with a numeric value but contains trailing non-numeric characters, and
an E_WARNING is emitted when the string does not contain a numeric
value.

I'm guessing either $item['quantity'] or $product['price'] does not contain a numeric value, so make sure that they do before trying to multiply them. Maybe use some sort of conditional before calculating the $sub_total, like so:

<?php

if (is_numeric($item['quantity']) && is_numeric($product['price'])) {
$sub_total += ($item['quantity'] * $product['price']);
} else {
// do some error handling...
}

Error_log: PHP Warning: A non-numeric value encountered

Change

$total = '';

to

$total = 0;

You're trying to add a number to a string, but the string doesn't look like a number. The empty string is converted to the number 0, so you get the correct total, but it also produces a warning.

PHP 7.4.21 - A non-numeric value encountered

$anglesStr += $angle;

This is a numeric addition, while you want string concatenation

$anglesStr .= $angle;

eg. 2+4

The first line would return 6 while second returns 24 (if valid data types).

A non-numeric value encountered with Laravel

Warning: A non-numeric value encountered

As @apokryfos said in a comment, don't concatenate strings with '+' sign. Use '.' instead.

i.e

// Use...
echo "Hello". " world! ". 508;

// Instead of...
echo "Hello"+ " world! "+ 508;

Notice: A non well formed numeric value encountered PHP

Try

return (float)number_format((float)str_replace(",", ".", (string) $b2Cost), 2, ".", "");

But if you just want to shorten the float, take a look at https://www.php.net/manual/en/function.round.php

warning a non-numeric value encountered in php

First, these are notices and warnings rather than errors. As you have seen, code execution will continue in these scenarios whereas script execution would terminate in case of an error.

There are at least two different issues here:

  1. You are accessing an array index that does not exist(line 66)
  2. You are using arithmetic operators on non-numeric values(line 67)

For 1, you should check that the key exists in the $_POST array before accessing it. I am assuming since it is absent in the request, the "save" parameter is optional. Since it is not present, null will be assigned to $save, so presumably this is the desired value for later logic. You can simply add a condition using array_key_exists or isset to prevent the notice. If you are you using php 7 or later you can use the "null coalesce" operator(??).

$save = array_key_exists($g, $_POST)?$_POST[$g]:null;

OR

$save = $_POST[$g]??null;

For 2, I would need more information about the values and intentions thereof. Judging by the addition operator and comma replacement, it appears that you are accepting numeric values that are formatted in some way. PHP will implicitly convert numeric strings to numbers in cases such as this. You may need to remove further formatting. Perhaps intval or floatval would be applicable depending on the input data. Ultimately, you need to extract the numbers that you need from the strings and convert them to numeric values before performing addition(unless you are attempting string concatenation, in which case see the . operator)

PHP Warning: A non-numeric value encountered need fix

what value has variable $stored_date,

as php says A non-numeric value

try smth like this:

$diff = (int)$duration - (int)$stored_date;


Related Topics



Leave a reply



Submit