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
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: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
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#L482)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.
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.
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
Hey,
I'd like to propose to make empty() a variadic, where if any
arguments passed in are considered empty, then false is returnedShould 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/
Hey,
I'd like to propose to make empty() a variadic, where if any
arguments passed in are considered empty, then false is returnedShould 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).
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/
Hi,
Hey,
I'd like to propose to make empty() a variadic, where if any
arguments passed in are considered empty, then false is returnedShould 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() whereTRUE
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