我已經研究並嘗試了很多解決方案,如何在foreach循環中正確調用函數,而且我仍然收到錯誤。以下是詳細內容:如何正確構建Foreach循環中的函數?
$firstName = "CA";
$firstNameArr = str_split($firstName);
foreach ($firstNameArr as $value){
function getLtr($ltr){
switch ($ltr) :
case "A": return 'The letter is A';
case "B": return 'The letter is B';
case "C": return 'The letter is C';
default: return 'This is not a valid selection';
endswitch;
}
echo getLtr($value) . '<br>';
}
我收到的錯誤:「不能重新聲明getltr()(先前宣佈...函數的第一行中引用)」
我感謝您的幫助!
你不能把一個函數定義一個循環 – GordonM