![](/img/trans.png)
[英]Strict Standards: Only variables should be passed by reference in… on line 777
[英]Strict Standards: Only variables should be passed by reference in… - error line
我有这样的错误:
严格的标准:仅在第100行的/file.php中通过引用传递变量
在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;
}
你可以帮帮我吗? 我发现了这样的主题,但中午有类似我的代码的代码。
end
通过引用接受其参数,因为它修改了数组。 即 它将数组迭代器移动到最后一个元素并返回最后一个元素。 这就是为什么您会收到警告。 如果仅替换产生错误的行,则可以将其删除:
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);
),但是不必这样做。
好吧,您可以使用更好的方法来获得像这样的扩展名
$ext = strrchr($file, ".");
唯一的区别是这保持了.
因此,如果$file
是somefile.txt
它将返回.txt
,如果您想摆脱它,可以总是这样做
$ext = ltrim(strrchr($file, "."), '.');
以供参考
http://php.net/manual/zh/function.strrchr.php
strrchr —查找字符串中字符的最后一次出现。
字符串 strrchr( 字符串 $ haystack, 混合 $ needle)
此函数返回干草堆的一部分,该部分从最后一次出现针时开始,一直到干草堆结束。
这样就找到了最后一个.
然后返回所有内容,然后ltrim
只是“左”修剪。
PS我真的不喜欢使用explode来获取扩展名,这是我的宠儿之一。
因此,对于您的特殊情况,我将:
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
你可以在这里在线测试
有一件事很明显,它的速度快将近2倍,这在这种情况下并不重要。 但是,检查这些东西永远不会有伤害。 两者都给出相同的结果,但是strrchr
更易于阅读,如果您仍然知道该函数的功能,则很好。 这是一种一个不起眼的功能,但它基本上意味着海峡 ING [R飞行CH A R ACTER。
干杯。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.