Heim  >  Artikel  >  Backend-Entwicklung  >  php eval函数用法总结_PHP教程

php eval函数用法总结_PHP教程

WBOY
WBOYOriginal
2016-07-21 15:15:171122Durchsuche

eval定义和用法

eval() 函数把字符串按照 PHP 代码来计算。

该字符串必须是合法的 PHP 代码,且必须以分号结尾。

如果没有在代码字符串中调用 return 语句,则返回 NULL。如果代码中存在解析错误,则 eval() 函数返回 false。

语法
eval(phpcode)
  
参数 描述
phpcode 必需。规定要计算的 PHP 代码。

提示和注释
注释:返回语句会立即终止对字符串的计算。
注释:该函数对于在数据库文本字段中供日后计算而进行的代码存储很有用。
例子

复制代码 代码如下:

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

输出:
代码如下复制代码 This is a $string $time morning!
This is a beautiful winter morning!
eval() 函数在CodeIgniter框架里也有用到。在 /system/database/DB.php 文件中,根据系统的配置动态的定义了一个类 CI_DB,具体代码片段如下:?
复制代码 代码如下:

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);
  

本函式可将字符串之中的变量值代入,通常用在处理数据库的数据上。参数 code_str 为欲处理的字符串。值得注意的是待处理的字符串要符合 PHP 的字符串格式,同时在结尾处要有分号。使用本函式处理后的字符串会沿续到 PHP 程序结束。

www.bkjia.comtruehttp://www.bkjia.com/PHPjc/326169.htmlTechArticleeval定义和用法 eval() 函数把字符串按照 PHP 代码来计算。 该字符串必须是合法的 PHP 代码,且必须以分号结尾。 如果没有在代码字符串中调...
Stellungnahme:
Der Inhalt dieses Artikels wird freiwillig von Internetnutzern beigesteuert und das Urheberrecht liegt beim ursprünglichen Autor. Diese Website übernimmt keine entsprechende rechtliche Verantwortung. Wenn Sie Inhalte finden, bei denen der Verdacht eines Plagiats oder einer Rechtsverletzung besteht, wenden Sie sich bitte an admin@php.cn