ArticleDirectory
Definition and usage
The eval () function follows the PHPCode.
The string must be a valid PHP code and must end with a semicolon.
If the return statement is not called in the code string, null is returned. If a parsing error exists in the Code, the eval () function returns false.
Syntax
Eval (phpcode)
| Parameters |
Description |
| Phpcode |
Required. Specifies the PHP code to be calculated. |
Tips and comments
Note:The return statement immediately terminates string calculation.
Note:This function is useful for storing code in Database Text fields for future computation.
Example
<? PHP $ string = "beautiful"; $ time = "Winter"; $ STR = 'this 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!
The eval () function is also useful 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:
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 );