eval是什么,相信很多都会知道。但真实有多少人使用它呢?恐怕在实际应用中,使用的也比较少吧。让我们看看手册上是怎么说的先:
- eval
- (PHP 3, PHP 4, PHP 5)
- eval -- Evaluate a string as PHP code
- 说明
- mixed eval ( string code_str )
- 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.
- 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.
- Also remember that variables given values under eval() will retain these values in the main script afterwards.
- 参数
- code_str
- The code string to be evaluated. code_str does not have to contain PHP Opening tags.
- A return statement will immediately terminate the evaluation of the string .
- 返回值
- 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.
- In PHP 3, eval() does not return a value.
- 注释
- 注意: 由于这是一个语言结构而非函数,因此它无法被变量函数调用。
- 提示: 为了防止程序直接将结果输出到浏览器,可以使用输出控制函数来捕获此函数的输出,并把它们保存到一个例如 string 类型的变量中。
- 注意: In case of a fatal error in the evaluated code, the whole script exits.
手册上也有一段例子,是为了演示用的:
- <?php
- $string = 'cup';
- $name = 'coffee';
- $str = 'This is a $string with my $name in it.';
- echo $str. "\n";
- eval("\$str = \"$str\";");
- echo $str. "\n";
- ?>
- 输出的结果如下:
- This is a $string with my $name in it.
- This is a cup with my coffee in it.
看了这个例子,相信很容易理解Evaluate a string as PHP code的含义了吧。正因为这样,所以eval()里的参数一定要有";"结尾,可以让PHP解释器知道这句PHP代码结束了。
上面是最常用的方法,当然也会有一些奇怪的用法:
- <?php
- eval('test');
- 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
- if( ! defined('MY_ARRAY') ){
- define( 'MY_ARRAY' , 'return ' . var_export( array( 1, 2, 3, 4, 5 ) , true ) . ';' );
- }
- $my_array = eval( MY_ARRAY );
- if( ! class_exists( 'my_class' ) ){
- class my_class {
- //private propreties
- var $_prop;
- var $_custom_check = 'return true;'; //of course, I want a default check code that return true
- //PHP4 constructor
- function my_class() {
- $this -> _prop = eval( MY_ARRAY );
- }
- function customize_check( $code ) {
- $this -> _custom_check = $code;
- }
- function check( $val ) {
- return eval( $this -> _custom_check );
- }
- }
- }
- $my_class = new my_class();
- $check = 'return in_array( $val , $this -> _prop , true );';
- $my_class -> customize_check( $check );
- print '<pre>';
- if( $my_class -> check( 1 ) ){
- echo '1 is checked as true.' . "\n";
- }
- else{
- echo '1 is checked as false.' . "\n";
- }
- //show: 1 is checked as true.
- if( $my_class -> check( '1' ) ){
- echo '"1" is checked as true.' . "\n";
- }
- else{
- echo '"1" is checked as false.' . "\n";
- }
- //show: "1" is checked as false.
- print '</pre>';