"严格标准:只应通过引用传递变量"错误

use*_*100 31 php

我试图在这里基于代码获得基于HTML的递归目录列表:

http://webdevel.blogspot.in/2008/06/recursive-directory-listing-php.html

代码运行正常,但它会引发一些错误:

严格标准:在第34行的C:\ xampp\htdocs\directory5.php中只能通过引用传递变量

严格标准:只应在第32行的C:\ xampp\htdocs\directory5.php中通过引用传递变量

严格标准:在第34行的C:\ xampp\htdocs\directory5.php中只能通过引用传递变量

以下是代码的摘录:

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";
  }
Run Code Online (Sandbox Code Playgroud)

hal*_*ush 56

这应该没问题

   $value = explode(".", $value);
   $extension = strtolower(array_pop($value));   //Line 32
   // the file name is before the last "."
   $fileName = array_shift($value);  //Line 34
Run Code Online (Sandbox Code Playgroud)

  • 刚刚遇到同样的警告,事实证明你可以通过在`explode()`周围使用额外的括号来保持代码内联:`$ fileName = array_shift((explode(".",$ value)));`. (10认同)
  • 所以它真的就像把事情放在事物之外一样简单......可以这么说:p (7认同)
  • @JamieHutber更多参与.array_pop的原型是混合的array_pop(array&$ array)注意参数中的&符号.这意味着数组输入参数通过引用而不是值传入.输入数组由一个元素缩短,即返回的元素,数组中的最后一个元素,从输入数组中删除.修改输入参数值的唯一方法是通过引用传递它.原始代码的表达式不能修改其值,因为它没有具有可引用值的命名内存位置. (7认同)

Shi*_*dim 24

array_shift唯一的参数是通过引用传递的数组.返回值explode(".", $value)没有任何参考.因此错误.

您应该首先将返回值存储到变量中.

    $arr = explode(".", $value);
    $extension = strtolower(array_pop($arr));   
    $fileName = array_shift($arr);
Run Code Online (Sandbox Code Playgroud)

来自PHP.net

以下内容可以通过引用传递:

- Variables, i.e. foo($a)
- New statements, i.e. foo(new foobar())
- [References returned from functions][2]
Run Code Online (Sandbox Code Playgroud)

不应该通过引用传递其他表达式,因为结果是未定义的.例如,以下通过引用传递的示例无效: