php function eval () Introduction and use example, eval example _php tutorial

Source: Internet
Author: User

php function eval () Introduction and use example, eval example


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.
If no return statement is called in the code string, NULL is returned. If there is a parsing error in the code, the eval () function returns FALSE.

Grammar

Eval (Phpcode)
Copy the Code code as follows:
Parameter description
Phpcode required. Specifies the PHP code to be computed.

Example 1
Copy the Code code as follows:
<?php
$string = "Beautiful";
$time = "Winter";

$str = ' This is a $string $time morning! ';
echo $str. "
";

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

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

Example 2

We use a For loop to create n random, and the value multiplies
Copy the 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. '. \ '
\' ;');
}

Echo '
';
Echo $a 1 + $a 10;

Output:
Copy the Code code as follows:
1
4
9
16
25
36
49
64
81
100
101


How to use the eval () function in PHP

Eval () must be a string in parentheses

If you write $a = 1; Eval ("Echo (\ $a +\ $b);"); Operation result is 1

If it is $a = 1; Eval ("Echo ($a + $b);"); The error

This means it's best to have a strict string and a full PHP statement.

$a = 1; eval ("echo \ $a;");
$a = 1; Eval ("echo $a;");
As a result, the first eval was performed with the Echo $a; And the second sentence is to perform echo 1;

How does PHP use the eval () function to change this instance?

$f = ' $ ';

$f. $a. $this->i
What this is, obviously, should not be the effect you want, which is equivalent to a string connection. Presumably you mean to do this, $ ($a. $this->i). Let's just think about the rest.

http://www.bkjia.com/PHPjc/866677.html www.bkjia.com true http://www.bkjia.com/PHPjc/866677.html techarticle php Function eval () describes and uses the example, eval example what is the eval () eval () function to calculate the string in PHP code. The string must be a valid PHP code, and must be a semicolon ...

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