Latest web development tutorials

PHP eval() 函數

PHP Misc 參考手冊 PHP Misc參考手冊

實例

把字符串當成PHP 代碼來計算:

<?php
$string = "beautiful";
$time = "winter";

$str = 'This is a $string $time morning!';
echo $str. "<br>";

eval("$str = "$str";");
echo $str;
?>

The output of the code above will be:

This is a $string $time morning!
This is a beautiful winter morning!


定義和用法

eval() 函數把字符串按照PHP 代碼來計算。

該字符串必須是合法的PHP 代碼,且必須以分號結尾。

註釋: return語句會立即終止對字符串的計算。

提示:該函數對於在數據庫文本字段中供日後計算而進行的代碼存儲很有用。


語法

eval( phpcode )

参数 描述
phpcode 必需。规定要计算的 PHP 代码。

技術細節

返回值: 除非在代碼字符串中調用return 語句,則返回傳給return 語句的值,否則返回NULL。 如果代碼字符串中存在解析錯誤,則eval() 函數返回FALSE。
PHP 版本: 4+


PHP Misc 參考手冊 PHP Misc參考手冊