If "=" exists in the eval function parameters of php, an error is reported. How can this problem be solved?

Source: Internet
Author: User
Tags parse error
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;');

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.