当前位置: 首页> 函数类别大全> eval

eval

把字符串作为PHP代码执行
名称:eval
分类:杂项
所属语言:php
一句话介绍:把字符串按照 PHP 代码来计算。

定义和用法

eval() 函数将字符串作为 PHP 代码来执行。

该字符串必须是有效的 PHP 代码,并且必须以分号结尾。

注意:如果字符串中包含 return 语句,它将会立即终止对字符串的求值。

提示:这个函数在将 PHP 代码存储在数据库中时可能会很有用。

实例

将字符串作为 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(phpcode)
参数 描述
phpcode 必需。规定要计算的 PHP 代码。
同类函数
热门文章