相比于eval,create_function也许是个更好的选择

编程语言
0 757

执行php表达式字符串,相比于eval,create_function也许是个更好的选择PHP eval() 函数定义和用法

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

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

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

语法
eval(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! 


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


//用create_functoin()创建匿名函数

//因为该函数已被弃用,部分编辑器会给出警告,多说无益

//知道这个函数曾经来过这个世界上就足够了

1

2

$func1 = create_function('$a,$b', 'return ($a+$b);');

echo $func1(10,20);

以上就是php中的eval()与create_function()的详细内容,更多请关注php中文网其它相关文章!