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

php
2022-08-30 14:07:48

我正在尝试基于此处的代码获取基于HTML的递归目录列表:

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

代码运行良好,但它会引发一些错误:

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

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

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

以下是代码摘录:

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";
  }

答案 1

这应该没问题

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

答案 2

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

应首先将返回值存储到变量。

    $arr = explode(".", $value);
    $extension = strtolower(array_pop($arr));   
    $fileName = array_shift($arr);

PHP.net

以下事情可以通过引用传递:

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

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


推荐