手机浏览 RSS 2.0 订阅 膘叔的简单人生 , 腾讯云RDS购买 | 超便宜的Vultr , 注册 | 登陆

PHP中Eval的作用

首页 > PHP >

eval是什么,相信很多都会知道。但真实有多少人使用它呢?恐怕在实际应用中,使用的也比较少吧。让我们看看手册上是怎么说的先:

 

XML/HTML代码
  1. eval   
  2. (PHP 3, PHP 4, PHP 5)   
  3.   
  4. eval -- Evaluate a string as PHP code   
  5. 说明   
  6. mixed eval ( string code_str )   
  7.   
  8.   
  9. Evaluates the string given in code_str as PHP code. Among other things, this can be useful for storing code in a database text field for later execution.    
  10.   
  11. There are some factors to keep in mind when using eval(). Remember that the string passed must be valid PHP code, including things like terminating statements with a semicolon so the parser doesn't die on the line after the eval(), and properly escaping things in code_str. To mix HTML output and PHP code you can use a closing PHP tag to leave PHP mode.    
  12.   
  13. Also remember that variables given values under eval() will retain these values in the main script afterwards.    
  14.   
  15. 参数   
  16.   
  17.   
  18. code_str   
  19. The code string to be evaluated. code_str does not have to contain PHP Opening tags.    
  20.   
  21. A return statement will immediately terminate the evaluation of the string .    
  22.   
  23.   
  24. 返回值   
  25. As of PHP 4, eval() returns NULL unless return is called in the evaluated code, in which case the value passed to return is returned. In case of a parse error in the evaluated code, eval() returns FALSE.    
  26.   
  27. In PHP 3, eval() does not return a value.    
  28.   
  29. 注释   
  30. 注意: 由于这是一个语言结构而非函数,因此它无法被变量函数调用。   
  31.   
  32. 提示: 为了防止程序直接将结果输出到浏览器,可以使用输出控制函数来捕获此函数的输出,并把它们保存到一个例如 string 类型的变量中。   
  33.   
  34. 注意: In case of a fatal error in the evaluated code, the whole script exits.    
  35.   

手册上也有一段例子,是为了演示用的:

 

PHP代码
  1. <?php   
  2. $string = 'cup';   
  3. $name = 'coffee';   
  4. $str = 'This is a $string with my $name in it.';   
  5. echo $str"\n";   
  6. eval("\$str = \"$str\";");   
  7. echo $str"\n";   
  8. ?>    
  9.   
  10. 输出的结果如下:   
  11. This is a $string with my $name in it.   
  12. This is a cup with my coffee in it.  

看了这个例子,相信很容易理解Evaluate a string as PHP code的含义了吧。正因为这样,所以eval()里的参数一定要有";"结尾,可以让PHP解释器知道这句PHP代码结束了。

上面是最常用的方法,当然也会有一些奇怪的用法:

 

PHP代码
  1. <?php   
  2.   
  3. eval('test');   
  4. eval('>' . 'test');  

直接运行第3行的代码肯定是报错,但是……第四行却又是正常的。因为"?>"让PHP代码结束了,整行代码的意思相当于 echo 'test';所以可以用类似的方法来输出其他不太适合用echo输出的内容,而又不影响代码的正常执行(比如,加载一个PHP文件,如果直接用echo(file_get_contents('aaa.php')))那么,输出的内容纯粹就是aaa.php的内容。而eval('?>'.file_get_contents('aaa.php'))则是输出aaa.php执行后的内容。

再来看看手册的评论里,老外(pierrotevrard at gmail dot com)还有这种更强劲的用法,当然,也非常的BT,仍然可以值得借鉴一下

PHP代码
  1. <?php   
  2. if( ! defined('MY_ARRAY') ){   
  3.   define( 'MY_ARRAY' , 'return ' . var_export( array( 1, 2, 3, 4, 5 ) , true ) . ';' );   
  4. }   
  5.   
  6. $my_array = eval( MY_ARRAY );   
  7.   
  8. if( ! class_exists'my_class' ) ){   
  9.   class my_class {   
  10.    //private propreties   
  11.    var $_prop;   
  12.    var $_custom_check = 'return true;'//of course, I want a default check code that return true   
  13.   
  14.    //PHP4 constructor   
  15.    function my_class() {   
  16.      $this -> _prop = eval( MY_ARRAY );   
  17.    }   
  18.    function customize_check( $code ) {   
  19.      $this -> _custom_check = $code;   
  20.    }   
  21.    function check( $val ) {   
  22.      return eval$this -> _custom_check );   
  23.    }   
  24.   }   
  25. }   
  26.   
  27. $my_class = new my_class();   
  28.   
  29. $check = 'return in_array( $val , $this -> _prop , true );';   
  30. $my_class -> customize_check( $check );   
  31.   
  32. print '<pre>';   
  33. if$my_class -> check( 1 ) ){   
  34.    echo '1 is checked as true.' . "\n";   
  35. }   
  36. else{   
  37.    echo '1 is checked as false.' . "\n";   
  38. }   
  39. //show: 1 is checked as true.   
  40.   
  41. if$my_class -> check( '1' ) ){   
  42.    echo '"1" is checked as true.' . "\n";   
  43. }   
  44. else{   
  45.    echo '"1" is checked as false.' . "\n";   
  46. }   
  47. //show: "1" is checked as false.   
  48. print '</pre>';  



本站采用创作共享版权协议, 要求署名、非商业和保持一致. 本站欢迎任何非商业应用的转载, 但须注明出自"易栈网-膘叔", 保留原始链接, 此外还必须标注原文标题和链接.

Tags: php

« 上一篇 | 下一篇 »

只显示10条记录相关文章

使用PHP得到所有的HTTP请求头 (浏览: 62783, 评论: 3)
我为什么会选用phpstorm (浏览: 52691, 评论: 5)
快速生成目录树 (浏览: 46838, 评论: 7)
通过file_get_contents来Post数据的实例 (浏览: 46402, 评论: 5)
PHP导入导出Excel方法 (浏览: 45225, 评论: 3)
PHP的XSS攻击过滤函数 (浏览: 42723, 评论: 2)
超详细:在Mac OS X中配置Apache + PHP + MySQL (浏览: 40384, 评论: 1)
PHP常见错误(二) (浏览: 39782, 评论: 1)
PHP sendmail (浏览: 37964, 评论: 7)
几个ZendStudio使用教程 (浏览: 36481, 评论: 0)

4条记录访客评论

好,很详细,总算明白了

Post by win on 2010, December 31, 11:49 AM 引用此文发表评论 #1

看不明白,哼!

Post by Spider on 2008, August 8, 4:27 PM 引用此文发表评论 #2

因为你有的时候实在没有办法知道一个值是关的,你不得不采用预估的方法。。。

Post by gouki on 2008, August 8, 10:38 AM 引用此文发表评论 #3

教主能讲讲老外这种BT写法有啥优点吗?

Post by 老妖怪 on 2008, August 8, 12:51 AM 引用此文发表评论 #4


发表评论

评论内容 (必填):