Eval () An interesting php function

Source: Internet
Author: User
Tags parse error

<?php
$str = ' echo ', ' hello ';
$content = eval ($str);
Echo ', word! ‘;
?>

Execution Result: hello,word!

First, the string as a PHP script processing. Believe this, we all know!

<?php
$str = ' Hello, world! echo "Hello,";
$content = eval ($str);
Echo ' word! ';
Execution Result:
/*
Parse error:syntax error, unexpected ' echo ' (T_echo) in E:\web\www\swoole_test\eval.php (4): eval () ' D code on line 1 word !
*/
?>

Second, when there is illegal PHP code in the string, error. I believe we all know it!

<?php
$str = ' Hello, world! echo "Hello,";
$content = eval ('?> '. $str); Note that the eval is now in Riga "? > "string
Echo ' word! ';
Execution Result:

/*
Hello, world! echo "Hello,"; word!
*/
?>

Three, at this time, there are illegal PHP code inside the string, but, no error.

---because the prefix "?>" (PHP Terminator), it has the following "string" All as a "string", right!

On the basis of (c), embedded in the string <?php ...? The > module is equivalent to embedding PHP code in an HTML file. What would it be like?

<?php
$str = ' Hello, world! <?php echo "Hello,";?> ';
$content = eval ('?> '. $str);
Echo ' word! ';
Execution Result:
/*
Hello, world! hello,word!
*/
?>

Ok! It will recognize the PHP module in the "string" and execute it!

The above example illustrates the role of eval ('?> ' $str) and eval ($STR).

In fact, the eval ($str) $str inside,

If the string contains a <?php ...? >,

Then the $STR string must be in <?php ...? > Precede with "?>" PHP Terminator.

Eval () An interesting php function

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.