# [DISCUSSION] Make empty() a Variadic

- Thread: https://externals.io/message/82549
- Original archive: https://news-web.php.net/php.internals/82549
- Started by Thomas Punt on 2015-02-12T18:55:09+00:00
- Messages: 16
- Reply by email to internals@lists.php.net
- More for agents: search with https://externals.io/search.md?q={query} · RFCs at https://externals.io/rfc.md · JSON API at https://externals.io/api · documentation at https://externals.io/llms.txt

---

## #82549 — Thomas Punt — 2015-02-12T18:55:09+00:00

URL: https://externals.io/message/82549#82549

Hello PHP Internals!
I'd like to propose to make empty() a variadic, where if any arguments passed in are considered empty, then false is returned - otherwise return true.
My reasoning for wanting this feature is as follows:1)It's a common scenario to want to check multiple expressions for empty values. I frequently see both of the following pieces of code in projects:    #1    if (empty($a) || empty($b) || empty($c)) {        // error here    }
    #2    if (!empty($a) && !empty($b) && !empty($c)) {        // all good!    }
Both of the above examples could be shortened if empty() was made to accept multiple arguments:    #1    if (empty($a, $b, $c)) {        // error here    }
    #2    if (!empty($a, $b, $c)) {        // all good!    }
This creates more compact code that is (in my oppinion, at least) easier to read.
Some code from real-world projects that could benefit from this feature:WordPress (one of many): https://github.com/WordPress/WordPress/blob/master/wp-admin/includes/template.php#L1963OpenCart: https://github.com/opencart/opencart/blob/45fc863fa068d82b5280890e6466a198faa54bff/upload/admin/controller/openbay/ebay_profile.php#L128phpbb: https://github.com/phpbb/phpbb/blob/040d451dcca9ae54d8f4b7bdd2f231033765a8f2/phpBB/phpbb/notification/method/jabber.php#L48

2)Users have brought up the want to pass in multiple arguments into empty() before, such as:http://stackoverflow.com/questions/4993104/using-ifempty-with-multiple-variables-phphttp://stackoverflow.com/questions/10950470/check-if-multiple-strings-are-empty
There have been solutions brought up by users to emulate a variadic empty, like [1][2], however for unset variables their solutions simply don't work.

So all in all, it seems like a simple feature to add for a short-hand notation of checking multiple expressions for emptiness (which seems like a common use-case for users). It has no BC implications and no real downsides (at least I couldn't think of any). I have created a patch [3], and if the feedback is positive, then I'll create an RFC and submit a PR.
Thanks,Tom
[1] http://stackoverflow.com/a/7798842/4530326[2] http://icoded.it/php-time-saving-function-to-check-multiple-variables-for-empty-values/[3] https://github.com/tpunt/php-src/commit/66c563829775770507147872b98320cdfcb6c51c

---

## #82550 — Kris Craig — 2015-02-12T19:21:40+00:00

URL: https://externals.io/message/82549#82550 · In reply to: #82549

> Hello PHP Internals!
> I'd like to propose to make empty() a variadic, where if any arguments
> passed in are considered empty, then false is returned - otherwise return
> true.
> My reasoning for wanting this feature is as follows:1)It's a common
> scenario to want to check multiple expressions for empty values. I
> frequently see both of the following pieces of code in projects:    #1
> if (empty($a) || empty($b) || empty($c)) {        // error here    }
>     #2    if (!empty($a) && !empty($b) && !empty($c)) {        // all
> good!    }
> Both of the above examples could be shortened if empty() was made to
> accept multiple arguments:    #1    if (empty($a, $b, $c)) {        //
> error here    }
>     #2    if (!empty($a, $b, $c)) {        // all good!    }
> This creates more compact code that is (in my oppinion, at least) easier
> to read.
> Some code from real-world projects that could benefit from this
> feature:WordPress (one of many):
> https://github.com/WordPress/WordPress/blob/master/wp-admin/includes/template.php#L1963OpenCart:
>
> https://github.com/opencart/opencart/blob/45fc863fa068d82b5280890e6466a198faa54bff/upload/admin/controller/openbay/ebay_profile.php#L128phpbb:
>
> https://github.com/phpbb/phpbb/blob/040d451dcca9ae54d8f4b7bdd2f231033765a8f2/phpBB/phpbb/notification/method/jabber.php#L48
>
> 2)Users have brought up the want to pass in multiple arguments into
> empty() before, such as:
> http://stackoverflow.com/questions/4993104/using-ifempty-with-multiple-variables-phphttp://stackoverflow.com/questions/10950470/check-if-multiple-strings-are-empty
> There have been solutions brought up by users to emulate a variadic empty,
> like [1][2], however for unset variables their solutions simply don't work.
>
> So all in all, it seems like a simple feature to add for a short-hand
> notation of checking multiple expressions for emptiness (which seems like a
> common use-case for users). It has no BC implications and no real downsides
> (at least I couldn't think of any). I have created a patch [3], and if the
> feedback is positive, then I'll create an RFC and submit a PR.
> Thanks,Tom
> [1] http://stackoverflow.com/a/7798842/4530326[2]
> http://icoded.it/php-time-saving-function-to-check-multiple-variables-for-empty-values/[3]
> https://github.com/tpunt/php-src/commit/66c563829775770507147872b98320cdfcb6c51c
>



I'd say go ahead and draft an RFC with all the details of your proposed
change, then we can discuss and vote on it.  On the surface, it looks like
a useful feature that wouldn't break any existing code.

--Kris

---

## #82551 — Thomas Punt — 2015-02-12T19:34:02+00:00

URL: https://externals.io/message/82549#82551

Hi Kris,

> Date: Thu, 12 Feb 2015 11:21:40 -0800
> From: kris.craig@gmail.com
> To: tpunt@hotmail.co.uk
> CC: internals@lists.php.net
> Subject: Re: [PHP-DEV] [DISCUSSION] Make empty() a Variadic
> 
> I'd say go ahead and draft an RFC with all the details of your proposed
> change, then we can discuss and vote on it.  On the surface, it looks like
> a useful feature that wouldn't break any existing code.
> 
> --Kris

Will do! Also, apologies for the email formatting - I didn't realise my message was going to be mangled when sent...

---

## #82581 — Maciej Sobaczewski — 2015-02-13T06:04:09+00:00

URL: https://externals.io/message/82549#82581 · In reply to: #82549

W dniu 2015-02-12 o 19:55, Thomas Punt pisze:
> Hello PHP Internals!
> I'd like to propose to make empty() a variadic, where if any arguments passed in are considered empty, then false is returned - otherwise return true.
> My reasoning for wanting this feature is as follows:1)It's a common scenario to want to check multiple expressions for empty values. I frequently see both of the following pieces of code in projects:    #1    if (empty($a) || empty($b) || empty($c)) {        // error here    }
>      #2    if (!empty($a) && !empty($b) && !empty($c)) {        // all good!    }
> Both of the above examples could be shortened if empty() was made to accept multiple arguments:    #1    if (empty($a, $b, $c)) {        // error here    }
>      #2    if (!empty($a, $b, $c)) {        // all good!    }
> This creates more compact code that is (in my oppinion, at least) easier to read.
> Some code from real-world projects that could benefit from this feature:WordPress (one of many): https://github.com/WordPress/WordPress/blob/master/wp-admin/includes/template.php#L1963OpenCart: https://github.com/opencart/opencart/blob/45fc863fa068d82b5280890e6466a198faa54bff/upload/admin/controller/openbay/ebay_profile.php#L128phpbb: https://github.com/phpbb/phpbb/blob/040d451dcca9ae54d8f4b7bdd2f231033765a8f2/phpBB/phpbb/notification/method/jabber.php#L48
>
> 2)Users have brought up the want to pass in multiple arguments into empty() before, such as:http://stackoverflow.com/questions/4993104/using-ifempty-with-multiple-variables-phphttp://stackoverflow.com/questions/10950470/check-if-multiple-strings-are-empty
> There have been solutions brought up by users to emulate a variadic empty, like [1][2], however for unset variables their solutions simply don't work.
>
> So all in all, it seems like a simple feature to add for a short-hand notation of checking multiple expressions for emptiness (which seems like a common use-case for users). It has no BC implications and no real downsides (at least I couldn't think of any). I have created a patch [3], and if the feedback is positive, then I'll create an RFC and submit a PR.
> Thanks,Tom
> [1] http://stackoverflow.com/a/7798842/4530326[2] http://icoded.it/php-time-saving-function-to-check-multiple-variables-for-empty-values/[3] https://github.com/tpunt/php-src/commit/66c563829775770507147872b98320cdfcb6c51c 		 	   		
>

I like the idea. It seems consistent with isset() being variadic as well.

---

## #82582 — Benoit Schildknecht — 2015-02-13T06:16:29+00:00

URL: https://externals.io/message/82549#82582 · In reply to: #82549

Hi,
Le Thu, 12 Feb 2015 19:55:09 +0100, Thomas Punt <tpunt@hotmail.co.uk> a  
écrit:

> Hello PHP Internals!
> I'd like to propose to make empty() a variadic, where if any arguments  
> passed in are considered empty, then false is returned - otherwise  
> return true.

I absolutely love this idea, and would make a good use of such an  
improvement.

Regards,
Benoit.

---

## #82583 — Michael Wallner — 2015-02-13T07:28:23+00:00

URL: https://externals.io/message/82549#82583 · In reply to: #82549

> I'd like to propose to make empty() a variadic, where if any
> arguments passed in are considered empty, then false is returned

Should that read "if any arguments passed in are considered *NOT* empty,
then false is returned"?


-- 
Regards,
Mike

---

## #82600 — Andrea Faulds — 2015-02-13T11:16:35+00:00

URL: https://externals.io/message/82549#82600 · In reply to: #82583

Hey,

> 
> 
> 
> 
> > […]
> 
> Should that read "if any arguments passed in are considered *NOT* empty,
> then false is returned”?

No, I think it’s correct, if confusingly phrased. I believe Thomas is proposing variadic empty() where `TRUE` is returned if any of its arguments are empty, otherwise FALSE. So, empty($a, $b, $c) would be equivalent to empty($a) || empty($b) || empty($c), much like isset($a, $b, $c) is equivalent to (and implemented as) isset($a) && isset($b) && isset($c).

--
Andrea Faulds
http://ajf.me/

---

## #82602 — Andrea Faulds — 2015-02-13T11:31:38+00:00

URL: https://externals.io/message/82549#82602 · In reply to: #82600

> 
> 
> Hey,
> 
> > […]
> 
> No, I think it’s correct, if confusingly phrased. I believe Thomas is proposing variadic empty() where `TRUE` is returned if any of its arguments are empty, otherwise FALSE. So, empty($a, $b, $c) would be equivalent to empty($a) || empty($b) || empty($c), much like isset($a, $b, $c) is equivalent to (and implemented as) isset($a) && isset($b) && isset($c).

Wait, I think I made a mistake.

* Thomas proposed "if any arguments passed in are considered empty, then false is returned”, i.e. !(empty($a) || empty($b) || empty($c)) if his words are taken literally. This doesn’t make much sense, I think it was a mistake.
* You suggested he may have meant "if any arguments passed in are considered *NOT* empty, then false is returned”, i.e. (empty($a) && empty($b) && empty($c))
* I assume Thomas actually meant “where if any arguments passed in are considered empty, then *true* is returned”, i.e. (empty($a) || empty($b) || empty($c))

Sorry for the confusion.

I think the || behaviour is the most useful, as it’s the analogue of isset’s. So !empty($a, $b, $c) would work similarly to isset($a, $b, $c), and similarly, !isset($a, $b, $c) would work similarly to empty($a, $b, $c).

But that’s just my opinion. :)
--
Andrea Faulds
http://ajf.me/

---

## #82604 — reeze — 2015-02-13T11:38:11+00:00

URL: https://externals.io/message/82549#82604 · In reply to: #82602

Hi,



>
> > […]
> proposing variadic empty() where `TRUE` is returned if any of its arguments
> are empty, otherwise FALSE. So, empty($a, $b, $c) would be equivalent to
> empty($a) || empty($b) || empty($c), much like isset($a, $b, $c) is
> equivalent to (and implemented as) isset($a) && isset($b) && isset($c).
>
> Wait, I think I made a mistake.
>
> * Thomas proposed "if any arguments passed in are considered empty, then
> false is returned", i.e. !(empty($a) || empty($b) || empty($c)) if his
> words are taken literally. This doesn't make much sense, I think it was a
> mistake.
> * You suggested he may have meant "if any arguments passed in are
> considered *NOT* empty, then false is returned", i.e. (empty($a) &&
> empty($b) && empty($c))
> * I assume Thomas actually meant "where if any arguments passed in are
> considered empty, then *true* is returned", i.e. (empty($a) || empty($b) ||
> empty($c))
>
> Sorry for the confusion.
>
> I think the || behaviour is the most useful, as it's the analogue of
> isset's. So !empty($a, $b, $c) would work similarly to isset($a, $b, $c),
> and similarly, !isset($a, $b, $c) would work similarly to empty($a, $b, $c).
>

For example: echo $a, $b, $c, & empty($a, $b, $c), they are treated equal,

if the empty() means if any one of them is empty then result is TRUE, the
advantage of it disappeared:

if (empty($a, $b, $c)) {
   // you might want to check it again.
   if (empty($a)) {
      //blah blah.
   } else if (empty($b)) {

   }
}


>
> But that's just my opinion. :)
> --
> Andrea Faulds
> http://ajf.me/
>
>
>
>
>
> --
> 
> 
>
>


-- 
Reeze Xia
http://reeze.cn

---

## #82606 — Thomas Punt — 2015-02-13T11:41:43+00:00

URL: https://externals.io/message/82549#82606

Hi,

> From: ajf@ajf.me>
> * I assume Thomas actually meant “where if any arguments passed in are considered empty, then *true* is returned”, i.e. (empty($a) || empty($b) || empty($c))
> 
> Sorry for the confusion.
> 
> I think the || behaviour is the most useful, as it’s the analogue of isset’s. So !empty($a, $b, $c) would work similarly to isset($a, $b, $c), and similarly, !isset($a, $b, $c) would work similarly to empty($a, $b, $c).
> 
> But that’s just my opinion. :)
> --
> Andrea Faulds
> http://ajf.me/

Yeah, my mistake. That's what I meant - empty will return `TRUE` if *any* of its arguments are considered empty, and `FALSE` otherwise. So the following two pieces of code are analogous:
# this:if (empty($a) || empty($b) || empty($c)) {// error here}
# is the same as this:if (empty($a, $b, $c)) {// error here}
# and this:if (!empty($a) && !empty($b) && !empty($c)) {// all good!}
# is the same as this:if (!empty($a, $b, $c)) {// all good!}
Sorry for the confusion :p
-Tom

---

## #82607 — Thomas Punt — 2015-02-13T12:11:54+00:00

URL: https://externals.io/message/82549#82607

Hi Reeze,

> From: reeze@php.net
> 
> For example: echo $a, $b, $c, & empty($a, $b, $c), they are treated equal,
> 
> if the empty() means if any one of them is empty then result is TRUE, the
> advantage of it disappeared:
> 
> if (empty($a, $b, $c)) {
> // you might want to check it again.
> if (empty($a)) {
> //blah blah.
> } else if (empty($b)) {
> 
> }
> }
> 


Having empty() return `TRUE` if *all* arguments are empty, and `FALSE` otherwise doesn't seem to be as useful (at least from what I've seen in code).


It's less common to see:

if (empty($a) && empty($b) && empty($c)) {}

than:

if (empty($a) || empty($b) || empty($c)) {}

Plus then the semantics wouldn't be inline with isset(), which could be somewhat confusing to users.

> 
> -- 
> Reeze Xia
> http://reeze.cn

Thanks,
Tom

---

## #82612 — Thomas Punt — 2015-02-13T13:45:58+00:00

URL: https://externals.io/message/82549#82612

Hi Francois,

> From: francois@tekwire.net>
> May I suggest to extend your proposal to `is_null()` with the same logic ?
If we were to do the same with `is_null()`, then would it not be best to do it with all the is_*() functions? I would be more than happy to cater for those functions as well, though their usages seem a lot less common. What does everyone else think?
> 
> François

Thanks, Tom

---

## #82613 — Sebastian B.-Hagensen — 2015-02-13T14:09:16+00:00

URL: https://externals.io/message/82549#82613 · In reply to: #82612

Hi,

2015-02-13 14:45 GMT+01:00 Thomas Punt <tpunt@hotmail.co.uk>:
> Hi Francois,
>
> > […]
> If we were to do the same with `is_null()`, then would it not be best to do it with all the is_*() functions? I would be more than happy to cater for those functions as well, though their usages seem a lot less common. What does everyone else think?

While I don't know how common such a usage is I'm certain that there
is a use case for it. However, just modifying a subset of the is_*
functions sounds like a bad idea. If such a change is applied it
should be done to all type related is_* functions and be  similar to
issets behavior (return false if at least one argument is not of the
given type).

Thanks,

---

## #82614 — Thomas Punt — 2015-02-13T14:22:08+00:00

URL: https://externals.io/message/82549#82614

Hi,

> Date: Fri, 13 Feb 2015 15:09:16 +0100
> From: sbj.ml.read@gmail.com
> 
> While I don't know how common such a usage is I'm certain that there
> is a use case for it. However, just modifying a subset of the is_*
> functions sounds like a bad idea. If such a change is applied it
> should be done to all type related is_* functions and be  similar to
> issets behavior (return false if at least one argument is not of the
> given type).
> 
> Thanks,

Agreed on that. It would be better for consistency's sake.
-Tom

---

## #82634 — Benoit Schildnecht — 2015-02-13T18:07:59+00:00

URL: https://externals.io/message/82549#82634 · In reply to: #82613

Hi,

""Sebastian B.-Hagensen""  a écrit dans le message de groupe de discussion : 
CAOjCV8yX7vOHTD1ja2oBDRntDZB0Q1SOYQdyhSaR3vt2vGSS+Q@mail.gmail.com...

>2015-02-13 14:45 GMT+01:00 Thomas Punt <tpunt@hotmail.co.uk>:
> > […]

> While I don't know how common such a usage is I'm certain that there
> is a use case for it. However, just modifying a subset of the is_*
> functions sounds like a bad idea. If such a change is applied it
> should be done to all type related is_* functions and be  similar to
> issets behavior (return false if at least one argument is not of the
> given type).

I agree, this behaviour should be extended to all the is_* functions if 
possible.

---

## #82641 — Thomas Punt — 2015-02-13T20:16:12+00:00

URL: https://externals.io/message/82549#82641

Hey Benoit,

> From: bensor987@neuf.fr
> Hi,
> 
> I agree, this behaviour should be extended to all the is_* functions if 
> possible. 
> 

I'm not sure we can do that for all of the is_* functions, since `is_callable()` accepts up to three arguments rather than taking only a single argument like the rest.
I'm also still waiting for karma for my wiki account (username: tpunt) to create an RFC for this thread - I don't suppose anyone could help me out there?
Thanks,Tom
