我試圖從book運行一些代碼。代碼看起來有問題。錯誤信息:致命錯誤:無法在寫入上下文中使用函數返回>值
以下是錯誤消息:
Fatal error: Can't use function return value in write context in /Applications/MAMP/htdocs/Eclipse-Workspace/simpleblog/test.php on line 24
這裏是在消息中所引用的代碼(關於第24行開始)
if (!empty(trim($_POST['username']))
&& !empty(trim($_POST['email']))) {
// Store escaped $_POST values in variables
$uname = htmlentities($_POST['username']);
$email = htmlentities($_POST['email']);
$_SESSION['username'] = $uname;
echo "Thanks for registering! <br />",
"Username: $uname <br />",
"Email: $email <br />";
}
我希望得到任何幫助。請讓我知道如果我需要提供更多信息
非常感謝你們。這非常快。該解決方案效果很好。
問題是empty()函數只需要應用於直接變量。
以供將來參考: 代碼是由賈森Lengstorf(2009年),90-91頁,第3章 '絕對初學者PHP',$ _SESSION
糾正代碼:
//new - Created a variable that can be passed to the empty() function
$trimusername = trim($_POST['username']);
//modified - applying the empty function correctly to the new variable
if (!empty($trimusername)
&& !empty($trimusername)) {
// Store escaped $_POST values in variables
$uname = htmlentities($_POST['username']);
$email = htmlentities($_POST['email']);
$_SESSION['username'] = $uname;
echo "Thanks for registering! <br />",
"Username: $uname <br />",
"Email: $email <br />";
}
非常感謝。這工作。如果有人使用本書並遇到問題,我已更新了更正後的代碼。 – ntc 2010-11-16 09:30:22
非常感謝您使用正確的代碼詢問和回答此問題。我是PHP和編程新手,當我在書中碰到一個錯字時,我幾乎總是認爲它只是我沒有得到它而已。把正確的東西放進去並看到它工作真是太好了! – 2011-02-15 21:28:08
我認爲應該指出,這種行爲隻影響PHP版本<5.5,如 [php.function.empty](http://php.net/manual/en/function.empty.php) – 2015-08-13 05:07:00