Eval Definition 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.
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)
Parameter description
Phpcode required. Specifies the PHP code to be computed.
Hints and Notes
Note: The return statement terminates the calculation of the string immediately.
Note: This function is useful for code storage that is made available for future calculations in a database text field.
Example
Copy CodeThe 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 below copies the code 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, with the following code snippet:?
Copy CodeThe 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 value of a variable in a string, and is typically used to manipulate the data on the database. The parameter code_str is the string to be processed. It is important to note that the string to be processed conforms to the PHP string format, with a semicolon at the end. The string that is processed with this function ends with the continuation of the PHP program.
http://www.bkjia.com/PHPjc/326169.html www.bkjia.com true http://www.bkjia.com/PHPjc/326169.html techarticle eval definition and usage the eval () function calculates the string in PHP code. The string must be a valid PHP code and must end with a semicolon. If there is no tone in the code string ...