简体   繁体   English

严格的标准:只有变量应通过引用传递给…-错误行

[英]Strict Standards: Only variables should be passed by reference in… - error line

I have error like this: 我有这样的错误:

Strict Standards: Only variables should be passed by reference in /file.php in line 100 严格的标准:仅在第100行的/file.php中通过引用传递变量

In file.php line look like this: 在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;
                }

Could you help me? 你可以帮帮我吗? I found topics like this but noon have code like my code. 我发现了这样的主题,但中午有类似我的代码的代码。

end accepts its parameter by reference, because it modifies the array. end通过引用接受其参数,因为它修改了数组。 I. e. it moves the array iterator to the last element and returns the last element. 它将数组迭代器移动到最后一个元素并返回最后一个元素。 That's why you get the warning. 这就是为什么您会收到警告。 You can remove it, if you simply replace the line that produces the error: 如果仅替换产生错误的行,则可以将其删除:

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

Replace with: 用。。。来代替:

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

Like you did in a later part of your code. 就像您在代码的后面部分所做的一样。

Then you could also delete line 105 ( $ext = explode(".", $file); ), but you don't have to. 然后,您也可以删除第105行( $ext = explode(".", $file); ),但是不必这样做。

Well you could use something better to get the extension like this 好吧,您可以使用更好的方法来获得像这样的扩展名

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

The only difference is this keeps the . 唯一的区别是这保持了. so if $file is somefile.txt it return .txt which if you want to get rid of you could always do this 因此,如果$filesomefile.txt它将返回.txt ,如果您想摆脱它,可以总是这样做

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

For reference 以供参考

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

strrchr — Find the last occurrence of a character in a string. strrchr —查找字符串中字符的最后一次出现。

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

This function returns the portion of haystack which starts at the last occurrence of needle and goes until the end of haystack. 此函数返回干草堆的一部分,该部分从最后一次出现针时开始,一直到干草堆结束。

So it just finds the last . 这样就找到了最后一个. and return that and everything after, then ltrim is just "left" trim. 然后返回所有内容,然后ltrim只是“左”修剪。

PS I really dislike using explode for getting the extension, it's one of my pet peeves. PS我真的不喜欢使用explode来获取扩展名,这是我的宠儿之一。

So for your particular case I would: 因此,对于您的特殊情况,我将:

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

This way, you get the extension one time, you don't create an array for it explode , you don't move the array pointer to the end of the array end and that's about it. 这样,您一次获得了扩展名,您没有为其explode创建数组,也没有将数组指针移到数组end就此。

UPDATE 更新

You can compare these two using microtime to see which is faster, they are both really fast so we have to do about 100k iterations to test it, like this: 您可以使用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;

Outputs 产出

 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

You can test it here online 你可以在这里在线测试

One thing is clear, it's almost 2x faster, not that it matters much in this case. 有一件事很明显,它的速度快将近2倍,这在这种情况下并不重要。 But, it never hurts to check these things. 但是,检查这些东西永远不会有伤害。 Both give equivalent results, but strrchr is easier to read, well if you know what the function does anyway. 两者都给出相同的结果,但是strrchr更易于阅读,如果您仍然知道该函数的功能,则很好。 It's kind of an obscure function but it basically means str ing r ight ch a r acter. 这是一种一个不起眼的功能,但它基本上意味着海峡 ING [R飞行CH A R ACTER。

Cheers. 干杯。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM