PHP Issue - A non-numeric value encountered
This error is encountered when we are going to do addition with different types of variables. See following example
$a = "";
$b = 3;
try{
$b = 3;
try{
$c = $a + $b;
}catch(\Exception $e){
var_dump($e->getMessage());
}
Exception is occurred. Error is "A non-numeric value encountered"
There are two solutions
Solution 1
Change the value of variable $a into zero (0). So, both variables are getting the same type of value now.
$a = 0;
$b = 3;
try{
try{
$c = $a + $b;
echo $c;
}catch(\Exception $e){
var_dump($e->getMessage());
}
Solution 2
You can simply cast $a when doing addition
$a = "";
$b = 3;
try{
try{
$c = (int) $a + $b;
echo $c;
}catch(\Exception $e){
var_dump($e->getMessage());
}
Enjoy..
Comments