Home > Backend Development > PHP Tutorial > Summary of usage of php eval function_PHP tutorial

Summary of usage of php eval function_PHP tutorial

WBOY
Release: 2016-07-21 15:15:17
Original
1200 people have browsed it

eval definition and usage

eval() function calculates string according to PHP code.

The string must be valid PHP code and must end with a semicolon.

If no return statement is called in the code string, NULL is returned. If there are parsing errors in the code, the eval() function returns false.

Syntax
eval(phpcode)
 
Parameter Description
phpcode required. Specifies the PHP code to be calculated.

Tips and Notes
Note: The return statement immediately terminates the evaluation of the string.
Comment: This function is useful for storing code in a database text field for later calculations.
Example

Copy code The code is as follows:

$string = "beautiful";
$time = "winter";
$str = 'This is a $string $time morning!';
echo $str. "
";
eval(" $str = "$str";");
echo $str;
?>  

Output:
The code is as follows Copy the code This is a $string $time morning !
This is a beautiful winter morning!
The eval() function is also used in the CodeIgniter framework. In the /system/database/DB.php file, a class CI_DB is dynamically defined according to the system configuration. The specific code snippet is as follows:?
Copy code The code is as follows:

if ( ! isset($active_record) OR $active_record == TRUE)
{
require_once(BASEPATH.'database/DB_active_rec.php');
if ( ! class_exists('CI_DB'))
{
eval('class CI_DB extends CI_DB_active_record { }');
}
}
else
{
if ( ! class_exists('CI_DB'))
{
eval('class CI_DB extends CI_DB_driver { }');
}
}
require_once(BASEPATH.'database/drivers/'.$ params['dbdriver'].'/'.$params['dbdriver'].'_driver.php');
// Instantiate the DB adapter
$driver = 'CI_DB_'.$params['dbdriver '].'_driver';
$DB = new $driver($params);
 

This function can substitute the variable value in the string, usually used in Process database data. The parameter code_str is the string to be processed. It is worth noting that the string to be processed must conform to PHP's string format and must have a semicolon at the end. The string processed using this function will be continued until the end of the PHP program.

www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/326169.htmlTechArticleeval definition and usage The eval() function calculates the string according to the PHP code. The string must be valid PHP code and must end with a semicolon. If not called in the code string...
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template