PHP eval() 函数
PHP eval() 函数
实例
把字符串当成 PHP 代码来计算:
<?php
$string = "beautiful";
$time = "winter";
$str = 'This is a $string $time morning!';
echo $str. PHP_EOL;
eval("\$str = \"$str\";");
echo $str;
?>
以上代码执行输出结果为:
This is a $string $time morning!
This is a beautiful winter morning!
This is a beautiful winter morning!
定义和用法
eval() 函数把字符串按照 PHP 代码来计算。
该字符串必须是合法的 PHP 代码,且必须以分号结尾。
注释:return 语句会立即终止对字符串的计算。
提示:该函数对于在数据库文本字段中供日后计算而进行的代码存储很有用。
语法
eval(phpcode)
参数 | 描述 |
---|---|
phpcode | 必需。规定要计算的 PHP 代码。 |
技术细节
返回值: | 除非在代码字符串中调用 return 语句,则返回传给 return 语句的值,否则返回 NULL。如果代码字符串中存在解析错误,则 eval() 函数返回 FALSE。 |
---|---|
PHP 版本: | 4+ |

相关文章
- PHP EOF(heredoc) 使用说明
- PHP 数组排序
- PHP While 循环
- PHP 多维数组
- PHP 错误处理
- PHP 过滤器
- PHP array_fill() 函数
- PHP array_fill_keys() 函数
- PHP array_map() 函数
- PHP array_rand() 函数
- PHP array_reverse() 函数
- PHP array_udiff() 函数
- PHP array_uintersect_assoc() 函数
- PHP natsort() 函数
- PHP reset() 函数
- PHP 5 Array 函数
- PHP 5 Calendar 函数
- PHP cURL 函数
- PHP 5 Date/Time 函数
- PHP 5 Math 函数