php 只有变量应该通过引用传递
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/4636166/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Only variables should be passed by reference
提问by Frank Nwoko
// Other variables
$MAX_FILENAME_LENGTH = 260;
$file_name = $_FILES[$upload_name]['name'];
//echo "testing-".$file_name."<br>";
//$file_name = strtolower($file_name);
$file_extension = end(explode('.', $file_name)); //ERROR ON THIS LINE
$uploadErrors = array(
0=>'There is no error, the file uploaded with success',
1=>'The uploaded file exceeds the upload max filesize allowed.',
2=>'The uploaded file exceeds the MAX_FILE_SIZE directive that was specified in the HTML form',
3=>'The uploaded file was only partially uploaded',
4=>'No file was uploaded',
6=>'Missing a temporary folder'
);
Any ideas? After 2 days still stuck.
有任何想法吗?2天后仍然卡住。
回答by Oswald
Assign the result of explode
to a variable and pass that variable to end
:
将 的结果分配explode
给变量并将该变量传递给end
:
$tmp = explode('.', $file_name);
$file_extension = end($tmp);
The problem is, that end
requires a reference, because it modifies the internal representation of the array (i.e. it makes the current element pointerpoint to the last element).
问题是,这end
需要引用,因为它修改了数组的内部表示(即它使当前元素指针指向最后一个元素)。
The result of explode('.', $file_name)
cannot be turned into a reference. This is a restriction in the PHP language, that probably exists for simplicity reasons.
的结果explode('.', $file_name)
不能转化为参考。这是 PHP 语言中的一个限制,可能出于简单原因而存在。
回答by Sinan Eldem
Php 7 compatible proper usage:
PHP 7 兼容正确用法:
$fileName = 'long.file.name.jpg';
$tmp = explode('.', $fileName);
$fileExtension = end($tmp);
echo $fileExtension;
// jpg
回答by ryeguy
Everyone else has already given you the reason you're getting an error, but here's the best way to do what you want to do:
$file_extension = pathinfo($file_name, PATHINFO_EXTENSION);
其他人都已经向您提供了出现错误的原因,但这是执行您想做的事情的最佳方法:
$file_extension = pathinfo($file_name, PATHINFO_EXTENSION);
回答by Floern
save the array from explode() to a variable, and then call end() on this variable:
将explode()中的数组保存到一个变量中,然后在这个变量上调用end():
$tmp = explode('.', $file_name);
$file_extension = end($tmp);
btw: I use this code to get the file extension:
顺便说一句:我使用此代码来获取文件扩展名:
$ext = substr( strrchr($file_name, '.'), 1);
where strrchr
extracts the string after the last .
and substr
cuts off the .
wherestrrchr
提取最后一个之后的字符串.
并substr
切断.
回答by Will Vousden
Try this:
尝试这个:
$parts = explode('.', $file_name);
$file_extension = end($parts);
The reason is that the argument for end
is passed by reference, since end
modifies the array by advancing its internal pointer to the final element. If you're not passing a variable in, there's nothing for a reference to point to.
原因是 for 的参数end
是通过引用传递的,因为end
通过将其内部指针前进到最后一个元素来修改数组。如果你没有传入一个变量,就没有什么可以指向的引用。
See end
in the PHP manual for more info.
有关end
更多信息,请参阅PHP 手册。
回答by wizard
PHP complains because end()
expects a reference to something that it wants to change (which can be a variable only). You however pass the result of explode()
directly to end()
without saving it to a variable first. At the moment when explode()
returns your value, it exists only in memory and no variable points to it. You cannot create a reference to something (or to something unknown in the memory), that does not exists.
PHP 抱怨是因为end()
期望引用它想要更改的内容(只能是变量)。但是,您可以explode()
直接将结果传递给 toend()
而无需先将其保存到变量中。在explode()
返回值的那一刻,它只存在于内存中,没有变量指向它。你不能创建对不存在的东西(或内存中未知的东西)的引用。
Or in other words: PHP does not know, if the value you give him is the direct value or just a pointer to the value (a pointer is also a variable (integer), which stores the offset of the memory, where the actual value resides). So PHP expects here a pointer (reference) always.
或者换句话说:PHP不知道,你给他的值是直接值还是只是一个指向该值的指针(指针也是一个变量(整数),它存放的是内存的偏移量,实际值在哪里居住)。所以 PHP 总是希望这里有一个指针(引用)。
But since this is still just a notice (not even deprecated) in PHP 7, you can savely ignore notices and use the ignore-operator instead of completely deactivating error reporting for notices:
但是由于这在 PHP 7 中仍然只是一个通知(甚至没有被弃用),您可以节省地忽略通知并使用 ignore-operator 而不是完全停用通知的错误报告:
$file_extension = @end(explode('.', $file_name));
回答by Tgr
回答by jon_darkstar
Just as you can't index the array immediately, you can't call end on it either. Assign it to a variable first, then call end.
正如你不能立即索引数组一样,你也不能调用 end 。先赋值给一个变量,然后调用end。
$basenameAndExtension = explode('.', $file_name);
$ext = end($basenameAndExtension);
回答by NVRM
Since it raise a flag for over 10 years, but works just fine and return the expected value, a little stfu operatoris the goodiest bad practice you are all looking for:
由于它已升旗超过 10 年,但工作正常并返回预期值,因此一点stfu 运算符是您正在寻找的最糟糕的做法:
$file_extension = @end(explode('.', $file_name));
回答by evenvi
PHP offical Manual : end()
PHP 官方手册:end()
Parameters
参数
array
The array. This 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.
阵列。这个数组是通过引用传递的,因为它被函数修改了。这意味着您必须向它传递一个真正的变量,而不是一个返回数组的函数,因为只有实际变量才能通过引用传递。