For example, a php sentence like this
eval("$a=1");
This error can occur at run time
Notice: Undefined variable: a in D:\test.php on line 4Parse error: syntax error, unexpected '=', expecting end of file in D:\test.php(4) : eval()'d code on line 1PHP Notice: Undefined variable: a in D:\test.php on line 4PHP Parse error: syntax error, unexpected '=', expecting end of file in D:\test.php(4) : eval()'d code on line 1
What's going on? How to solve?
Reply content:
For example, a php sentence like thiseval("$a=1");
This error can occur at run time
Notice: Undefined variable: a in D:\test.php on line 4Parse error: syntax error, unexpected '=', expecting end of file in D:\test.php(4) : eval()'d code on line 1PHP Notice: Undefined variable: a in D:\test.php on line 4PHP Parse error: syntax error, unexpected '=', expecting end of file in D:\test.php(4) : eval()'d code on line 1
What's going on? How to solve?
This is because the variable names in the double quotes are escaped.
PHP allows us to include string variables directly in a double-quote string, and the variables in the double-quote string are interpreted and replaced, and the contents of the single-quote string are always considered ordinary characters. For example:
So a $ A in the string "$a = 1" is not recognized as a string, but instead is treated as a variable, and PHP will then replace its interpretation. But $ A is not defined, so the variable is not defined . After the $a is replaced, it becomes a literal value, assigning a number to a constant, which of course prompts for a syntax error .
It is good practice to change the double quotation marks to single quotes eval("$a=1;");
, or to escape eval("\$a=1;");
.
Also note that the semicolon at the end of the string statement should not be forgotten, which is one of the reasons for the error.
eval("\$a=1");
Example:
1
eval("$a=1");
改成 eval('$a=1;');