当前位置: 移动技术网 > IT编程>开发语言>PHP > 浅谈PHP eval()函数定义和用法

浅谈PHP eval()函数定义和用法

2017年12月12日  | 移动技术网IT编程  | 我要评论

沙龙国际娱乐城,养生保健论坛,塞林尼尔

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

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

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

语法

eval(phpcode) 

 

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

 提示和注释

注释:返回语句会立即终止对字符串的计算。

注释:该函数对于在数据库文本字段中供日后计算而进行的代码存储很有用。

例子

<?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!

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

以上这篇浅谈php eval()函数定义和用法就是小编分享给大家的全部内容了,希望能给大家一个参考,也希望大家多多支持移动技术网。

如对本文有疑问,请在下面进行留言讨论,广大热心网友会与你互动!! 点击进行留言回复

相关文章:

验证码:
移动技术网