PHP eval() 函數(shù)

2018-04-23 15:21 更新

PHP eval() 函數(shù)

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

實例

把字符串當(dāng)成 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() 函數(shù)把字符串按照 PHP 代碼來計算。

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

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

提示:該函數(shù)對于在數(shù)據(jù)庫文本字段中供日后計算而進行的代碼存儲很有用。


語法

eval(phpcode)

參數(shù) 描述
phpcode 必需。規(guī)定要計算的 PHP 代碼。

技術(shù)細(xì)節(jié)

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


PHP Misc 參考手冊 PHP Misc 參考手冊
以上內(nèi)容是否對您有幫助:
在線筆記
App下載
App下載

掃描二維碼

下載編程獅App

公眾號
微信公眾號

編程獅公眾號