php “严格标准:只应通过引用传递变量”错误
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/9848295/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
"Strict Standards: Only variables should be passed by reference" error
提问by user1184100
I am trying to get an HTML-based recursive directory listing based on code here:
我正在尝试根据此处的代码获取基于 HTML 的递归目录列表:
http://webdevel.blogspot.in/2008/06/recursive-directory-listing-php.html
http://webdevel.blogspot.in/2008/06/recursive-directory-listing-php.html
Code runs fine but it throws some errors:
代码运行良好,但会引发一些错误:
Strict Standards: Only variables should be passed by reference in C:\xampp\htdocs\directory5.php on line 34
Strict Standards: Only variables should be passed by reference in C:\xampp\htdocs\directory5.php on line 32
Strict Standards: Only variables should be passed by reference in C:\xampp\htdocs\directory5.php on line 34
严格的标准:在 C:\xampp\htdocs\directory5.php 第 34 行,只应通过引用传递变量
严格标准:在 C:\xampp\htdocs\directory5.php 中的第 32 行,只应通过引用传递变量
严格的标准:在 C:\xampp\htdocs\directory5.php 第 34 行,只应通过引用传递变量
Below is the excerpt of code:
下面是代码的摘录:
else
{
// the extension is after the last "."
$extension = strtolower(array_pop(explode(".", $value))); //Line 32
// the file name is before the last "."
$fileName = array_shift(explode(".", $value)); //Line 34
// continue to next item if not one of the desired file types
if(!in_array("*", $fileTypes) && !in_array($extension, $fileTypes)) continue;
// add the list item
$results[] = "<li class=\"file $extension\"><a href=\"".str_replace("\", "/", $directory)."/$value\">".$displayName($fileName, $extension)."</a></li>\n";
}
回答by haltabush
This should be OK
这应该没问题
$value = explode(".", $value);
$extension = strtolower(array_pop($value)); //Line 32
// the file name is before the last "."
$fileName = array_shift($value); //Line 34
回答by Shiplu Mokaddim
array_shift
the only parameter is an array passed by reference. The return value of explode(".", $value)
does not have any reference. Hence the error.
array_shift
唯一的参数是一个通过引用传递的数组。的返回值explode(".", $value)
没有任何参考。因此错误。
You should store the return value to a variable first.
您应该首先将返回值存储到变量中。
$arr = explode(".", $value);
$extension = strtolower(array_pop($arr));
$fileName = array_shift($arr);
From PHP.net
来自PHP.net
The following things can be passed by reference:
可以通过引用传递以下内容:
- Variables, i.e. foo($a)
- New statements, i.e. foo(new foobar())
- [References returned from functions][2]
No other expressions should be passed by reference, as the result is undefined. For example, the following examples of passing by reference are invalid:
不应通过引用传递其他表达式,因为结果未定义。例如,以下按引用传递的示例是无效的:
回答by Adrian Fernando Alconera
I had a similar problem.
我有一个类似的问题。
I think the problem is that when you try to enclose two or more functions that deals with an array type of variable, php will return an error.
我认为问题在于,当您尝试包含两个或多个处理数组类型变量的函数时,php 会返回错误。
Let's say for example this one.
让我们说例如这个。
$data = array('key1' => 'Robert', 'key2' => 'Pedro', 'key3' => 'Jose');
// This function returns the last key of an array (in this case it's $data)
$lastKey = array_pop(array_keys($data));
// Output is "key3" which is the last array.
// But php will return “Strict Standards: Only variables should
// be passed by reference” error.
// So, In order to solve this one... is that you try to cut
// down the process one by one like this.
$data1 = array_keys($data);
$lastkey = array_pop($data1);
echo $lastkey;
There you go!
你去吧!