rfc:variadic_empty

PHP RFC: Make empty() a Variadic

Introduction

This RFC aims make empty() have a variable arity.

// example current usage #1:
if (empty($a) || empty($b) || empty($c)) {}
 
// example current usage #2:
if (!empty($a) && !empty($b) && !empty($c)) {}
 
// new proposed usage #1:
if (empty($a, $b, $c)) {}
 
// new proposed usage #2:
if (!empty($a, $b, $c)) {}

Proposal

The proposal is to change empty() so that it can accept multiple arguments. This will enable developers to write more compact code when checking for the emptiness of multiple expressions.

As the above snippet demonstrates, the semantics of a variadic empty() should be the equivalent to logically OR'ing together multiple empty() invocations. Thus, if any arguments passed into empty() are considered falsy, then true will be returned; if no arguments are considered empty, then false is returned. This behaviour is the most logical (given empty()'s falsy semantics) and seems to be the most prevalent usage of multiple empty checks in user-land code (therefore being the most beneficial behaviour).

Justification

In PHP, it is not uncommon to see conditionals consisting of multiple empty() invocations. This is evident by simply browsing through some popular open source projects:

WordPress 1):

if ( empty( $pointer_id ) || empty( $selector ) || empty( $args ) || empty( $args['content'] ) )

OpenCart 2):

if (empty($setting['dispatch_times']) || empty($setting['countries']) || empty($setting['returns'])){

phpBB 3):

return !(
	empty($this->config['jab_enable']) ||
	empty($this->config['jab_host']) ||
	empty($this->config['jab_username']) ||
	empty($this->config['jab_password']) ||
	!@extension_loaded('xml')
);

And so on...

So this seems like quite a common need for users, and one that cannot be emulated in user-land code because of empty()'s behaviour of suppressing undefined variable errors.

This change will also make empty() more inline with the not-too-dissimilar isset(), which is good for POLA.

Backward Incompatible Changes

No BC breakages.

Proposed PHP Version(s)

PHP 7.0

Unaffected PHP Functionality

The current functionality of empty() will be completely preserved.

Vote

Because this is a language change, a 2/3 majority is required. It is a simple yes/no vote on whether empty() should be made a variadic.

Make empty() a Variadic
Real name Yes No
aharvey (aharvey)  
ajf (ajf)  
auroraeosrose (auroraeosrose)  
bishop (bishop)  
brandon (brandon)  
bwoebi (bwoebi)  
crodas (crodas)  
Damien Tournoud (damz)  
danack (danack)  
derick (derick)  
diegopires (diegopires)  
dmitry (dmitry)  
dragoonis (dragoonis)  
duodraco (duodraco)  
eliw (eliw)  
fredemmott (fredemmott)  
frozenfire (frozenfire)  
galvao (galvao)  
gasolwu (gasolwu)  
guilhermeblanco (guilhermeblanco)  
gwynne (gwynne)  
ircmaxell (ircmaxell)  
jedibc (jedibc)  
jgmdev (jgmdev)  
jmikola (jmikola)  
kguest (kguest)  
kinncj (kinncj)  
krakjoe (krakjoe)  
lcobucci (lcobucci)  
leigh (leigh)  
mbeccati (mbeccati)  
mfischer (mfischer)  
mfonda (mfonda)  
mike (mike)  
ocramius (ocramius)  
pajoye (pajoye)  
pauloelr (pauloelr)  
peehaa (peehaa)  
philstu (philstu)  
pollita (pollita)  
ramsey (ramsey)  
rdohms (rdohms)  
rmf (rmf)  
salathe (salathe)  
santiagolizardo (santiagolizardo)  
sebastian (sebastian)  
stas (stas)  
stelianm (stelianm)  
svpernova09 (svpernova09)  
yohgaki (yohgaki)  
yunosh (yunosh)  
zeev (zeev)  
Final result: 26 26
This poll has been closed.

Voting starts on 2015-03-07 and ends on 2015-03-21.

Patches and Tests

rfc/variadic_empty.txt · Last modified: 2017/09/22 13:28 by 127.0.0.1