php function eval () Introduction and use Example _php instance

Source: Internet
Author: User

What is eval ()?

The eval () function calculates the string according to the PHP code.
The string must be a valid PHP code and must end with a semicolon.
Returns NULL if the return statement is not invoked in the code string. If there is a parse error in the code, the eval () function returns FALSE.

Grammar

Eval (Phpcode)

Copy Code code as follows:

Parameter description
Phpcode required. Specify the PHP code to be computed.

Example 1

Copy Code code as follows:

<?php
$string = "Beautiful";
$time = "Winter";

$str = ' is a $string $time morning! ';
echo $str. "<br/>";

Eval ("\ $str = \" $str \ ";");
Echo $str;
?>

Output:
This is a $string $time morning!
This is a beautiful winter morning!

Example 2

We use the For loop to create n random, and the values multiply

Copy Code code as follows:

<?php

For ($i =1 $i <=10; $i + +) {
Eval (' $a '. $i. ' = '. $i * $i).
}

For ($i =1 $i <=10; $i + +) {
Eval (' echo $a '. $i. ' <br/>\ '; ');
}

echo ' <br/> ';
Echo $a 1 + $a 10;

Output:
Copy Code code as follows:

1
4
9
16
25
36
49
64
81
100
101

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.