Take the State of PHP 2026 Survey →

Allow arbitrary expression arguments to empty() and isset() View as Markdown

Implemented (PHP 5.5)
Authors
Nikita Popov nikic@php.net
Date
2012-04-12
Source
wiki.php.net/rfc/empty_isset_exprs

Votes

Which of the language constructs should accept arbitrary arguments? closed
Both empty() and isset()
3
Only empty()
14
None
4
21 voters
ab Only empty() 2012-05-03 08:19
brianlmoon Both empty() and isset() 2012-05-01 18:24
cataphract None 2012-05-02 09:12
colder Only empty() 2012-04-30 12:50
dragoonis Only empty() 2012-05-01 16:18
drak Only empty() 2012-05-01 11:21
hholzgra Only empty() 2012-05-05 10:53
hradtke None 2012-05-03 13:51
ircmaxell Both empty() and isset() 2012-05-01 16:34
kassner Only empty() 2012-04-30 13:18
lstrojny Only empty() 2012-05-21 20:35
lynch None 2012-05-04 16:49
mfonda Only empty() 2012-04-30 14:06
mj Only empty() 2012-05-08 05:07
nikic Only empty() 2012-04-30 12:49
pajoye Only empty() 2012-05-03 05:47
patrickallaert Only empty() 2012-04-30 13:30
rdohms Only empty() 2012-05-02 14:33
salathe None 2012-05-09 14:48
stas Both empty() and isset() 2012-05-02 08:49
weierophinney Only empty() 2012-04-30 13:57
Should empty() and isset() accept arbitrary arguments? closed
yes
12
no
2
14 voters
cataphract yes 2012-04-19 09:16
colder yes 2012-04-30 11:28
ircmaxell yes 2012-04-19 14:07
kassner yes 2012-04-29 12:04
kriscraig yes 2012-04-28 21:11
laruence yes 2012-04-29 10:20
mike yes 2012-04-19 12:09
mj yes 2012-04-22 11:51
neufeind yes 2012-04-19 13:45
nikic yes 2012-04-18 23:06
patrickallaert no 2012-04-30 07:51
salathe no 2012-04-28 22:51
stas yes 2012-04-19 17:58
tyrael yes 2012-04-29 11:31

Discussions on the mailing list

[RFC] Allow non-variable arguments to empty() and isset()
by Nikita Popov · 30 messages
[VOTE] Allow non-variable arguments to empty()
by Nikita Popov · 9 messages
[VOTE] Vote change for empty() RFC
by Nikita Popov · 43 messages

RFC text

Copied from wiki.php.net, last synchronized Oct 13, 2026 11:52 UTC

Summary

This RFC proposes to allow arbitrary expressions as arguments to empty() and isset().

What is the current behavior?

Currently empty() and isset() only accept variables as arguments. Thus it is possible to write empty($foo), but it is not possible to write empty(foo()).

Trying to do so results in this not particularly helpful error message: “Can't use function return value in write context”.

For other expressions (not variables and not function calls) a parse error is thrown.

Why was this behavior chosen in the past?

Using empty() and isset() on a non-variable was disallowed previously, as the main purpose of these language constructs is to suppress an error message in case the passed variable does not exist.

As for function calls (and other expressions) it is already known that the value exists, using empty()/isset() is not necessary and !func()/func() !== null can be used instead.

Why should we change the behavior?

Even though !func() and empty(func()) would behave exactly the same, the latter is more readable in certain contexts. For example if func() is expected to return an array, it feels more natural to verify it's emptiness using empty() instead of !.

Furthermore the current behavior often is unexpected to newbies. empty() and isset() look like functions, so programmers new to PHP expect them to accept any value (in particular function call results).

Patch

The patch is available as a PR on Github: https://github.com/php/php-src/pull/54

Change only empty()?

After further discussion it seems like it might be better to only add expression support for empty(), but not for isset(). The original RFC included isset(), because changing only one of the language constructs seemed inconsistent (as they are so similar).

On the other hand, using isset() on function calls and other expressions doesn't seem particularly useful. isset(someFunction())is semantically unclear (how can someFunction() not be set?) and could be interpreted as a check whether the function itself exists.

Allowing isset() to accept expressions would probably only cause confusion.

Vote

Vote: Which of the language constructs should accept arbitrary arguments? — Both empty() and isset(): 3 · Only empty(): 14 · None: 4 (closed)

The previous vote is obsolete and is left here only for reference:

Vote: Should empty() and isset() accept arbitrary arguments? — yes: 12 · no: 2 (closed)

Result

The RFC was accepted with the option “Only empty()”. The relevant commit it https://github.com/php/php-src/commit/ec061a93c53c8cde10237741e98e992c1a05d148.