我有一個問題,使用此代碼以獲得適當的結果。未定義的變量使用函數功能
我的代碼:
function check_txt($url, $content) {
global $content;
$result = get_file_contents($url);
$arr = array_filter($result, function($ar) {
return ($ar['txt'] == $content);
});
return $arr[0];
}
當我執行的代碼,我得到這個錯誤:
Notice: Undefined variable: content in myfile.php
我的問題是如何將內容變量傳遞給function($ar)
?已經嘗試過function($ar, $content)
以及global $content;
,就像我發佈的代碼一樣。
http://php.net/manual/en/functions.anonymous.php#example-203 –
'$ content'是不是在匿名函數的範圍。在這種情況下你可以使用'use'。 –
@PaulCrovella謝謝你,這解決了我的問題。 –