For example, if such a PHP statement eval (& quot; $ a1 & quot;); such an error occurs at runtime {code...}, what is the problem? How can this problem be solved? For example, a PHP statement
eval("$a=1");
This error occurs during running.
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 can this problem be solved?
Reply content:
For example, a PHP statementeval("$a=1");
This error occurs during running.
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 can this problem be solved?
This is because the variable name in double quotation marks is escaped.
PHP allows us to directly include string variables in double quotation marks. Variables in double quotation marks will be interpreted and replaced, and the content in single quotation marks is always considered as common characters. For example:
$ Foo = 2; echo "foo is $ foo"; // print the result: foo is 2 echo 'foo is $ foo'; // print the result: foo is $ foo echo "foo is $ foo \ n"; // print the result: foo is 2 (line feed) echo 'foo is $ foo \ n '; // print the result: foo is $ foo \ n
Therefore, $ a in the string "$ a = 1" is not recognized as a string, but is treated as a variable. PHP will replace its interpretation. But $ a is not defined, so a prompt is displayed.Variable Not Defined. After $ a is replaced, it becomes a literal value, and a number is assigned to a constant. Of course, a prompt is displayed.Syntax Error.
It is better to change double quotation marks to single quotation marks.eval("$a=1;");
, Or escapeeval("\$a=1;");
.
NoteSemicolonDon't forget, this is also one of the reasons for the error.
eval("\$a=1");
Example:
1
eval("$a=1");
Changeeval('$a=1;');