Eval Definitions and usage
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)
Parameter description
Phpcode required. Specify the PHP code to be computed.
Tips and comments
Note: The return statement immediately terminates the calculation of the string.
Note: This function is useful for storing code in database text fields for future calculations.
Example
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:
The code is copied as follows this is a $string $time morning!
This is a beautiful winter morning!
The eval () function is also useful in the CodeIgniter framework. In the/system/database/db.php file, a class ci_db is defined dynamically according to the configuration of the system, and the specific code fragment is as follows:?
Copy Code code 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 be used to substitute the value of a variable in a string, usually on data that is processed on the database. Parameter code_str is the string to be processed. It is worth noting that the string to be processed conforms to the PHP string format, with a semicolon at the end. The string that is processed using this function is followed by the end of the PHP program.