This is an old revision of the document!
PHP RFC: standard built-in is_empty() function
- Version: 0.1
- Date: 2023-10-30
- Author: Alessandro Rosa, alessandro.a.rosa@gmail.com
- Status: Under Discussion
- First Published at: http://wiki.php.net/rfc/empty_function
Introduction
So far the goal of the standard function `empty' is, according to the official documentation, to “determine whether a variable is empty”. There are kinds of flaws that should be settled, in my viewpoint:
1) the semantics: the function name is ambiguous about the action, that is, it is not clear whether the goal of empty() function should consist of testing the `emptiness' of input variable or return an empty version of the input variable.
2) the behavior. The only two issues come from inputing no variable or managing the false/true constant values. I resumed the behavior here below:
echo "built-in function `empty'<br/>"; echo "null value: ".( empty( null ) ? "Yes" : "No" )."<br/>"; echo "undeclared var : ".( empty( $i ) ? "Yes" : "No" )."<br/>"; //echo "empty input : ".( empty() ? "Yes" : "No" )."<br/>"; //empty input var will let code crash echo "empty string : ".( empty( "" ) ? "Yes" : "No" )."<br/>"; echo "empty array : ".( empty( [] ) ? "Yes" : "No" )."<br/>"; $i = 1; echo "boolean false : ".( empty( false ) ? "Yes" : "No" )."<br/>"; // expected value : 1 echo "boolean true : ".( empty( true ) ? "Yes" : "No" )."<br/>"; // expected value : 1 echo "declared var : ".( empty( $i ) ? "Yes" : "No" )."<br/>"; // expected value : 1 echo "non-empty string : ".( empty( "something" ) ? "Yes" : "No" )."<br/>"; // expected value : 1 echo "non empty array : ".( empty( [10] ) ? "Yes" : "No" )."<br/>"; // expected value : 1
Proposal
First, the `empty' name shall be adequated to the `is_' family of php built-in functions (is_null, is_bool, is_string, is_array, ...). Hence I propose `is_empty'. The return value will be true for emptiness and false otherwise. If the input variable is not empty would throw an error.
Second, I implemented the following version which fixes the above two flaws:
function is_empty( $input = null ) { $ser = @serialize( $input ); if ( preg_match( "/^N;$/i", $ser ) === 1 ) return 1; if ( preg_match( "/^b\:[01]\;?$/i", $ser ) === 1 ) return 1; return preg_match( "/0\:(\{\}|\[\]|\"\")\;?$/i", $ser ) === 1 ? 1 : 0; }
I resorted to the `serialize' built-in function just to show how it should work. This is just an escamotage coded in PHP for obtaining a formal basis and checking input variable consistence in the examples below. However, I presume that the internal C-version would approach the emptiness test in better ways that PHP is not able to perform. Hence note that I suppressed warnings in order to let this function run throughout the following tests, that also cover undefined variables:
echo "null value: ".is_empty( null )."<br/>"; // expected return value : 0 echo "undeclared var : ".is_empty( $i )."<br/>"; // expected return value : 0 echo "empty input : ".is_empt()."<br/>"; // expected return value : 0 echo "empty string : ".is_empty( "" )."<br/>"; // expected return value : 0 echo "empty array : ".is_empty( [] )."<br/>"; // expected return value : 0 echo "<br/><br/>"; $i = 1; echo "boolean false : ".is_empty( false )."<br/>"; // expected return value : 1 echo "boolean true : ".is_empty( true )."<br/>"; // expected return value : 1 echo "declared var : ".is_empty( $i )."<br/>"; // expected return value : 1 echo "non-empty string : ".is_empty( "something" )."<br/>"; // expected return value : 1 echo "non empty array : ".is_empty( [10] )."<br/>"; // expected return value : 1
Code works correctly and it can be also finely read.
Proposed PHP Version(s)
next PHP 8.(x+2) [suggested] The status of empty() function shall be set to `deprecated'. The new is_empty() function will co-live with the older one until the latter will be definitely superseeded after 2 versions (suggest deadline) from the official embedding in the standard library.
RFC Impact
To SAPIs
None
To Existing Extensions
None
To Opcache
None
Please explain how you have verified your RFC's compatibility with opcache.
New Constants
None
php.ini Defaults
None
Open Issues
None
Unaffected PHP Functionality
Just the current nomenclature of standard built-in empty() function.
Future Scope
None
Proposed Voting Choices
We will probably vote for or against adding these functions. This requires 2/3 majority.
Patches and Tests
None
Implementation
Refer to the above code.
References
No refs or links
Rejected Features
None.