php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Request #77469 Support for multiple empty() arguments - like isset()
Submitted: 2019-01-16 14:37 UTC Modified: 2019-01-21 12:31 UTC
From: rich at milns dot com Assigned:
Status: Suspended Package: Variables related
PHP Version: 7.2.14 OS: All
Private report: No CVE-ID: None
Have you experienced this issue?
Rate the importance of this bug to you:

 [2019-01-16 14:37 UTC] rich at milns dot com
Description:
------------
It would be useful if the empty() function also supported multiple function arguments, like isset() does. 

So basically "are all of these variables empty()".

Similar to: Bug #12978: Multiple isset

Test script:
---------------
e.g. 

<?php
// multiple variables supported by empty()
var_dump(empty($var1, $var2, $var3)); // true
?>

instead of:

<?php
// repeatedly calling empty() to check multiple variables
$result = (empty($var1) and empty($var2) and empty($var3));
var_dump($result); // true
?>


Patches

Add a Patch

Pull Requests

Add a Pull Request

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2019-01-16 20:13 UTC] cmb@php.net
-Package: Documentation problem +Package: Variables related
 [2019-01-18 11:22 UTC] nikic@php.net
-Status: Open +Status: Suspended
 [2019-01-18 11:22 UTC] nikic@php.net
There has been an RFC for this feature, which was declined: https://wiki.php.net/rfc/variadic_empty

The reason why this RFC failed is basically that empty($a, $b) must desugar to empty($a) && empty($b), while isset($a, $b) desugars to isset($a) || isset($b). Some people felt that this is confusing.

I'm suspending this ticket, as a new RFC proposal would be necessary to accept this feature.
 [2019-01-18 11:23 UTC] nikic@php.net
Ooops, I mixed up the logic ops. I meant that empty($a, $b) desugars to empty($a) || empty($b) and isset($a, $b) desugars to isset($a) && isset($b).
 [2019-01-20 12:51 UTC] rich at milns dot com
Thanks for the update. I'm not quite sure I understand why empty() and isset() desugar differently?

RE: isset() The manual says "If multiple parameters are supplied then isset() will return TRUE only if all of the parameters are set."

For empty I would propose "If multiple parameters are supplied then empty() will return TRUE only if all of the parameters are not empty."

That way, both behave in a similar way. 

To me, it would be most logical to assume that multiple isset() and multiple empty() would require all arguments to resolve to true in order to return true. 

If you only wanted to check that at least one of the multiple arguments provided to isset() or empty() are true then I would expect another function to be called instead, such as any_isset() or any_empty() for example.

In my opinion empty() should return TRUE if all multiple arguments provided to the function are "empty". 

If you need to mix and match (e.g. "$x is empty OR $y is empty") then I would not expect to be able to pass multiple arguments to empty(). 

I agree that the RFC you linked to was incorrect, but do you think a new RFC along these lines would be acceptable?
 [2019-01-21 12:31 UTC] nikic@php.net
> I agree that the RFC you linked to was incorrect, but do you think a new RFC along these lines would be acceptable?

The semantics in the RFC are correct. The easiest way to understand why it must work this way is to consider that isset($a, $b) should be basically the same as !empty($a, $b) (modulo the distinction of null vs falsy values).

    isset($a, $b) ==> isset($a) && isset($b)
    !empty($a, $b) ==> !empty($a) && !empty($b)

By de Morgans law, this means that empty($a, $b) is empty($a) || empty($b).

An RFC that does not follow these semantics would not be accepted.
 
PHP Copyright © 2001-2024 The PHP Group
All rights reserved.
Last updated: Tue Apr 23 16:01:30 2024 UTC