php eval()
定義和用法
eval() 函數把字符串按照 PHP 代碼來計算。
該字符串必須是合法的 PHP 代碼,且必須以分號結尾。
如果沒有在代碼字符串中調用 return 語句,則返回 NULL。如果代碼中存在解析錯誤,則 eval() 函數返回 false。
語法
eval(phpcode)
參數 | 描述 |
---|---|
phpcode | 必需。規定要計算的 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!