Home > php教程 > php手册 > body text

PHP eval() function, phpeval function

WBOY
Release: 2016-07-06 14:25:16
Original
1357 people have browsed it

PHP eval() function, phpeval function

Definition and usage

The eval() function evaluates strings 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.

Grammar

?
eval(phpcode)

 

参数 描述
phpcode 必需。规定要计算的 PHP 代码。

Tips and Notes

Note: The return statement immediately terminates the calculation of the string.

Comments: This function is useful for storing code in a database text field for later calculations.

Example

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

输出:

This is a $string $time morning! This is a beautiful winter morning!  
Copy after login

<br />eval() 函数在CodeIgniter框架里也有用到。在 /system/database/DB.php 文件中,根据系统的配置动态的定义了一个类 CI_DB,具体代码片段如下:
Copy after login
?
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);

  

Related labels:
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 Recommendations
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!