【问题标题】:"Strict Standards: Only variables should be passed by reference" error [duplicate]“严格标准:只能通过引用传递变量”错误[重复]
【发布时间】:2012-04-08 13:11:52
【问题描述】:

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

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

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

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

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

严格标准:只有变量才能通过引用传递 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";
  }

【问题讨论】:

    标签: php


    【解决方案1】:

    应该没问题

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

    【讨论】:

    • 所以它真的就像把东西放在外面一样简单......可以这么说:p
    • @JamieHutber 差不多,是的。我可以确认建议的解决方案适用于 PHP 5.3.27。
    • @JamieHutber 稍微多一些参与。 array_pop 的原型是混合 array_pop ( array &$array ) 注意参数中的 & 符号。这意味着数组输入参数是通过引用而不是值传递的。输入数组被缩短一个元素,即返回的元素,数组中的最后一个元素,从输入数组中移除。修改输入参数值的唯一方法是通过引用传递它。原始代码中的表达式无法修改其值,因为它没有可以引用的值的命名内存位置。
    • 刚刚遇到同样的警告,事实证明您可以通过在explode():$fileName = array_shift((explode(".", $value))); 周围使用额外的括号来保持代码内联。
    • @spenibus。从 PHP 7 开始,额外的括号不会阻止警告。请参阅php.net/manual/en/migration70.incompatible.php 的“函数参数周围的括号不再影响行为”部分
    【解决方案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]
    

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

    【讨论】:

      【解决方案3】:

      我也遇到过类似的问题。

      我认为问题在于,当你尝试将两个或多个处理数组类型变量的函数括起来时,php 会返回错误。

      比如说这个。

      $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;
      

      你去!

      【讨论】:

      • 你是对的。事实上,这些事情确实会导致错误和警告。
      【解决方案4】:

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

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

      【讨论】:

        猜你喜欢
        • 2015-02-02
        • 2012-10-25
        • 2017-07-08
        • 2017-07-24
        • 1970-01-01
        • 2013-05-21
        • 1970-01-01
        相关资源
        最近更新 更多