我有一組php函數,用於在錯誤文本失敗時返回錯誤文本。他們沒有返回文本。我的功能看起來是這樣的:從功能返回的PHP打印字符串
function poop(){
$stuff = stuff;
$things = things;
if($stuff != stuff){
return 'e: stuff does not equal stuff!';
}
if($things != things){
return 'e: things do not equal things!';
}
// if we got this far all is good!
return true;
}
我把我的函數代碼如下所示:
if(poop() === true){
// do things that require poop
} else {
echo poop();
}
我認爲這將返回「E:」字符串作爲一個字符串,並將其打印在頁面poop()沒有返回布爾真值的事件,但事實上並非如此。我想知道這是否是因爲我的代碼的其餘部分的錯誤,或者如果這個功能實際上不存在於PHP中?我應該如何確保我的所有函數檢查都返回錯誤?我應該使用echo而不是return來將它打印成頁面上的一個字符串嗎?
Protip:'foo'或'bar',而不是'poop'。 –
我使用poop作爲變量。 – FrankieTheKneeMan
不明白這個問題,但無論如何你應該做'$ result = poop(); if($ result === true){} else {echo $ result;}'而不是調用func兩次 – tigrang