为什么 strtolower() 不遵循 PHP 严格标准

Why strtolower() does not follow the PHP strict Standard

如果我使用

strtolower(end(explode('.',$_FILES['file']['name'])));

它给我错误

PHP Strict Standards: Only variables should be passed by reference in

我想好吧,我只是先将值存储在一个变量中,然后使用 explode

 $filename = $_FILES['file']['name'];
 $filearray = explode('.',$filename);

而且效果很好

但是我还有另一条线

strtolower(end($filearray));

我认为它应该给我同样的错误,我的意思是我应该首先将 end($filearray) 存储在一个变量中,然后在 strtolower()

中使用该变量

但这并没有给我任何错误,那么为什么 strtolower() 接受一个函数作为参数,却没有给出错误,有人可以解释为什么吗?

发出警告的不是 strtolower,而是 end 函数。引用 docs:

end() advances array's internal pointer to the last element, and returns its value. [...] The array is passed by reference because it is modified by the function. This means you must pass it a real variable and not a function returning an array because only actual variables may be passed by reference.

在您的第一个示例中,您尝试 end explode 调用的结果 - 即,不是真正的变量。虽然 PHP 可以忽略这样的用例,但它 通常 意味着你做错了什么 - 并且 E_STRICT 警告尝试通知你它。

您的第三个示例运行良好,因为:

1) strtolower其实并不在乎引用。它 returns 一个字符串,所有字母字符都转换为小写,而不是就地修改字符串。

2) end 传入了一个变量 - 数组。它 returns 它的最后一个元素,同时将该数组的内部指针推进到它的末尾。您是否尝试过使用此内部指针(通过 current 或其他方式),您会看到不同之处。


作为旁注(已在 @DoktorOSwaldo), you can replace the all explode(end() stuff with simple pathinfo 的评论中提到:

$ext = strtolower(pathinfo($_FILES['file']['name'], PATHINFO_EXTENSION));

因为 php 中的某些函数作为引用传递。 end 是这些功能之一。见文件:http://php.net/manual/en/function.end.php

但是 strtolower 函数得到的只是一个普通参数。

那么为什么 end 函数会得到一个引用呢? End 不仅会 return 最后一个元素,还会将数组的内部指针移动到最后一个元素。因此,如果您在 end 函数之后调用 current 函数,您将获得最后一个元素。 所以基本上结束函数会修改传入参数的数组。因此它需要是一个可以修改的变量作为参考。