Twipped
Twipped

Reputation: 1179

PHP Fatal Error. Does empty() try to alter the results passed into it?

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( array('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?

Upvotes: 3

Views: 244

Answers (1)

Unicron
Unicron

Reputation: 7456

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).

Upvotes: 10

Related Questions