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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-26 07:46:24  来源:igfitidea点击:

"Strict Standards: Only variables should be passed by reference" error

php

提问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_shiftthe 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!

你去吧!

回答by Bakyt Abdrasulov

Instead of parsing it manually it's better to use pathinfofunction:

与其手动解析它,不如使用pathinfo函数:

$path_parts = pathinfo($value);
$extension = strtolower($path_parts['extension']);
$fileName = $path_parts['filename'];