[PHP]Only variables should be passed by reference

PHP報錯:Only variables should be passed by referencephp

A PHP Error was encountered  
Severity: Runtime Notice  
Message: Only variables should be passed by reference

$file_name = $_FILES[$upload_name]['name'];
$file_extension = end(explode('.', $file_name)); //ERROR ON THIS LINE數組

報錯緣由:

獲取後綴名時,使用end函數,將數組內部指針指向最後一個元素,而end是引用傳值。函數

The problem is, that end requires a reference, because it modifies the internal representation of the array (i.e. it makes the current element pointer point to the last element).ui

The result of explode('.', $file_name) cannot be turned into a reference. This is a restriction in the PHP language, that probably exists for simplicity reasons.指針

The array. This array is passed by reference because it is modified by the function. This means you must pass it a real variable and not a function returning an array because only actual variables may be passed by reference.rest

解決辦法:

(1) 定義一個變量獲取explode後的值,再使用end函數:code

$parts = explode('.', $file_name);  
$file_extension = end($parts);

(2) 使用substr和strrchr函數來取文件後綴名:ci

$ext = substr( strrchr($file_name, '.'), 1);

(3) 使用pathinfo函數來取文件後綴名:element

$file_ext = pathinfo($file_name, PATHINFO_EXTENSION);

end() 函數將數組內部指針指向最後一個元素,並返回該元素的值(若是成功)。
pathinfo() 函數以數組的形式返回文件路徑的信息。get

參考連接:

http://www.w3school.com.cn/php/func_array_end.asp
http://www.w3school.com.cn/php/func_filesystem_pathinfo.asp
http://stackoverflow.com/questions/4636166/only-variables-should-be-passed-by-reference

相關文章
相關標籤/搜索