background image

 

PHP

函数 php eval 函数用法总结

  这个函数我想很多朋友都知道,黑客用得最多的一句了,可以解析 php 代码并且运
行哦,

eval

是函数不可在 php 中禁止,以前我就以为可以在 php.ini 禁止此函数,结果失

败了

eval

 

定义和用法

 

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,具体代码片段如下:? 

 

复制代码 代码如下: