Warning:A non-numeric value encountered
case
<?php
$a = "";
var_dump($a >> 1);
Output for 7.1.0 - 7.1.4
Warning: A non-numeric value encountered in /in/U6hQP on line 4
int(0)
Output for 5.6.0 - 5.6.30, hhvm-3.12.14 - 3.19.0, 7.0.0 - 7.0.18
int(0)
<?php
$ops = explode(" ", "+ - * / ** % << >> | & ^");
$non_num = '"abc123"';
$non_well_formed_num = '"123abc"';
function expr($expr) {
ob_start();
$r = eval("return $expr;");
return [$r, trim(ob_get_clean())];
}
foreach($ops as $op) {
$n = rand(0,9);
$expr = "{$n} {$op} ({$non_num} + 1)";
list($r, $err) = expr("$expr");
echo "$expr = $r -------> $err\n";
$expr = "{$n} {$op} ({$non_well_formed_num} + 1)";
list($r, $err) = expr("$expr");
echo "$expr = $r -------> $err\n";
}
PHP VERSION >= 7.1.0
+ 1 是为了防止除零产生异常
Output for 7.1.3
9 + ("abc123" + 1) = 10 -------> Warning: A non-numeric value encountered in /in/U5OGM(9) : eval()'d code on line 1
9 + ("123abc" + 1) = 133 -------> Notice: A non well formed numeric value encountered in /in/U5OGM(9) : eval()'d code on line 1
0 - ("abc123" + 1) = -1 -------> Warning: A non-numeric value encountered in /in/U5OGM(9) : eval()'d code on line 1
0 - ("123abc" + 1) = -124 -------> Notice: A non well formed numeric value encountered in /in/U5OGM(9) : eval()'d code on line 1
6 * ("abc123" + 1) = 6 -------> Warning: A non-numeric value encountered in /in/U5OGM(9) : eval()'d code on line 1
6 * ("123abc" + 1) = 744 -------> Notice: A non well formed numeric value encountered in /in/U5OGM(9) : eval()'d code on line 1
1 / ("abc123" + 1) = 1 -------> Warning: A non-numeric value encountered in /in/U5OGM(9) : eval()'d code on line 1
1 / ("123abc" + 1) = 0.0080645161290323 -------> Notice: A non well formed numeric value encountered in /in/U5OGM(9) : eval()'d code on line 1
9 ** ("abc123" + 1) = 9 -------> Warning: A non-numeric value encountered in /in/U5OGM(9) : eval()'d code on line 1
9 ** ("123abc" + 1) = 2.1187083124089E+118 -------> Notice: A non well formed numeric value encountered in /in/U5OGM(9) : eval()'d code on line 1
5 % ("abc123" + 1) = 0 -------> Warning: A non-numeric value encountered in /in/U5OGM(9) : eval()'d code on line 1
5 % ("123abc" + 1) = 5 -------> Notice: A non well formed numeric value encountered in /in/U5OGM(9) : eval()'d code on line 1
9 << ("abc123" + 1) = 18 -------> Warning: A non-numeric value encountered in /in/U5OGM(9) : eval()'d code on line 1
9 << ("123abc" + 1) = 0 -------> Notice: A non well formed numeric value encountered in /in/U5OGM(9) : eval()'d code on line 1
4 >> ("abc123" + 1) = 2 -------> Warning: A non-numeric value encountered in /in/U5OGM(9) : eval()'d code on line 1
4 >> ("123abc" + 1) = 0 -------> Notice: A non well formed numeric value encountered in /in/U5OGM(9) : eval()'d code on line 1
9 | ("abc123" + 1) = 9 -------> Warning: A non-numeric value encountered in /in/U5OGM(9) : eval()'d code on line 1
9 | ("123abc" + 1) = 125 -------> Notice: A non well formed numeric value encountered in /in/U5OGM(9) : eval()'d code on line 1
5 & ("abc123" + 1) = 1 -------> Warning: A non-numeric value encountered in /in/U5OGM(9) : eval()'d code on line 1
5 & ("123abc" + 1) = 4 -------> Notice: A non well formed numeric value encountered in /in/U5OGM(9) : eval()'d code on line 1
7 ^ ("abc123" + 1) = 6 -------> Warning: A non-numeric value encountered in /in/U5OGM(9) : eval()'d code on line 1
7 ^ ("123abc" + 1) = 123 -------> Notice: A non well formed numeric value encountered in /in/U5OGM(9) : eval()'d code on line
说明
7.1.0版本引入:
字符串以数字开头,但是后续包含非数字字符,进行数学运算时会抛出E_NOTICE错误;
Notice: A non well formed numeric value encountered in %s on line %d
字符串不以数字开头,进行数学运算时会抛出E_WARNING错误;
Warning: A non-numeric value encountered in %s on line %d
string与number的隐式类型转换参见: http://php.net/manual/zh/language.types.string.php#language.types.string.conversion