【问题标题】:Strict Standards: Only variables should be passed by reference in... - error line严格的标准:只有变量应该通过引用传递... - 错误行
【发布时间】:2018-10-08 18:42:57
【问题描述】:

我有这样的错误:

严格标准:只有变量应该在 /file.php 第 100 行通过引用传递

在 file.php 行看起来像这样:

foreach($filelist as $value => $file) {
    // ABOVE LINE IS LINE 100
    if(in_array(end(explode(".", $file)), $extensions)&&is_file($dir.$file)) { $c++; }
    if(IsSet($_GET['start'])) {
        $nav = $_GET['start'];
    } else {
        $nav = "0";
    }
    if(($c > $nav) && ($c < $nav+($pagerows+1))) {
        $link = $dir . $file;
        $hlink = $http . $file;
        $ext = explode(".", $file);
        if(in_array(end($ext), $extensions)&&is_file($link)) {
            $p++;
            if(file_exists($link)) {
                list($width, $height, $type, $attr) = getimagesize($link);
                if($height > SMALL_IMAGE_HEIGHT) {
                    $imageheight = SMALL_IMAGE_HEIGHT;
                } else {
                    $imageheight = $height;
                }

你能帮帮我吗?我找到了这样的主题,但中午有代码像我的代码。

【问题讨论】:

  • 嗨,我已经改进了问题的格式(标记第 100 行的注释实际上不可见!)不过,我建议您阅读 stackoverflow.com/help/mcve 并根据本指南提问.好消息是,一旦您开始最小化重现问题的代码,您就更有可能自己找到问题的根源。此外,您应该将“像这样的主题”放在网站上,以便我们可以建议您忽略的主题。最好的问候

标签: php arrays variables standards strict


【解决方案1】:

end 通过引用接受其参数,因为它修改了数组。 IE。它将数组迭代器移动到最后一个元素并返回最后一个元素。这就是你收到警告的原因。如果您只是替换产生错误的行,则可以将其删除:

if(in_array(end(explode(".", $file)), $extensions)&&is_file($dir.$file)) { $c++; }

替换为:

$ext = explode(".", $file); 
if(in_array(end($ext), $extensions)&&is_file($dir.$file)) { $c++; }

就像你在代码的后面部分所做的那样。

那么您也可以删除第 105 行 ($ext = explode(".", $file);),但您不必这样做。

【讨论】:

  • 所以if(in_array(end(explode(".", $file)), 改为if(in_array(end($tmp = explode(".", $file)), ?
  • 完全正确。或者您可以使用$ext,就像您稍后在代码中所做的那样。
  • 我更改为if(in_array(end($ext = explode(".", $file)), $extensions)&amp;&amp;is_file($dir.$file)) { $c++; } 仍然显示错误
  • 我的错。我确信任务会像这样工作,但显然不是。我将编辑答案以更正它。
  • 现在答案应该是正确的。很抱歉造成混乱。
【解决方案2】:

你可以使用更好的东西来获得这样的扩展

$ext = strrchr($file, ".");

唯一的区别是这保留了.,所以如果$filesomefile.txt,它会返回.txt,如果你想摆脱它,你可以随时这样做

$ext = ltrim(strrchr($file, "."), '.');

供参考

http://php.net/manual/en/function.strrchr.php

strrchr — 查找字符串中最后一次出现的字符。

字符串 strrchr ( 字符串 $haystack , 混合 $needle )

此函数返回 haystack 中从最后一次出现 needle 开始一直到 haystack 结束的部分。

所以它只找到最后一个 . 并返回它以及之后的所有内容,然后 ltrim 只是“左”修剪。

附:我真的不喜欢使用爆炸来获取扩展,这是我最讨厌的事情之一。

所以对于你的特殊情况,我会:

foreach($filelist as $value => $file) {
    $ext = ltrim(strrchr($file, "."), '.');
    if(in_array($ext, $extensions) && is_file($dir.$file)) { $c++; } 

    if(isset($_GET['start'])) { $nav = $_GET['start']; } else { $nav = "0"; }
    if(($c > $nav)&&($c < $nav+($pagerows+1))) {
    $link = $dir . $file;
    $hlink = $http . $file;
      
    //$ext = explode(".", $file); we've already done this no need to do it again

这样,您获得了一次扩展,您无需为其创建数组explode,您无需将数组指针移动到数组的末尾end,仅此而已。

更新

您可以使用microtime 比较这两者,看看哪个更快,它们都非常快,所以我们必须进行大约 100k 次迭代来测试它,如下所示:

$filename = "filename.php";

$start = microtime(true);

for($i=0; $i<=100000; $i++){
    $v=explode(".", $filename);
    $ext1 = end($v);
}

echo "1. ext='$ext1' Complete ".number_format((microtime(true) - $start), 4).PHP_EOL;

$start = microtime(true);

for($i=0; $i<=100000; $i++){
    $ext2 = ltrim(strrchr($filename, '.'), '.');
    
}

echo "2. ext='$ext2' Complete ".number_format((microtime(true) - $start), 4).PHP_EOL;

输出

 1. ext='php' Complete 0.0178
 2. ext='php' Complete 0.0098
 ----------------------------
 1. ext='php' Complete 0.0237
 2. ext='php' Complete 0.0125
 ---------------------------
 1. ext='php' Complete 0.0252
 2. ext='php' Complete 0.0098
 ---------------------------
 1. ext='php' Complete 0.0190
 2. ext='php' Complete 0.0102

你可以测试一下here online

有一点很清楚,它的速度几乎快了 2 倍,但在这种情况下并不重要。但是,检查这些东西永远不会有什么坏处。两者都给出相同的结果,但strrchr 更易于阅读,如果您知道该函数的作用。这是一种晦涩难懂的功能,但它基本上意味着 string right character。

干杯。

【讨论】:

  • 那么你能写信给我吗,我应该在我的代码中替换什么?
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2014-07-13
  • 1970-01-01
  • 1970-01-01
  • 2016-01-12
  • 2012-10-25
  • 2023-03-06
相关资源
最近更新 更多