Ran into a strange problem in PHP today and I'm wondering if someone can explain it. While comparing two arrays I initially tried something like this:
echo empty(array_diff( ar开发者_JS百科ray('foo','bar') , array('bar','foo') ))
This results in the following error:
Fatal Error: Can't use function return value in write context
Rewriting this as...
$dif = array_diff( array('foo','bar') , array('bar','foo') );
echo empty($dif);
...works perfectly. Empty should just be evaluating the value passed in to it, not writing to it, so what's going wrong here? Tested in both PHP 5.2.10 and PHP 5.3.2.
I've resolved the issue by using !count()
instead of empty()
, but I'm curious why it doesn't work in the first place. Is empty()
trying to alter the result from array_diff
?
Check the manual on empty():
Note: empty() only checks variables as anything else will result in a parse error. In other words, the following will not work:
empty(trim($name))
.
empty()
, like e.g. echo()
and die()
, is a language construct, and therefore has different rules than a normal function (in which your example would work fine).
精彩评论