PHP in the eval function parameter exists "=" will error, how to resolve

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

  • 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.