rfc:empty_function

This is an old revision of the document!


PHP RFC: Your Title Here

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 return value : 1
echo "boolean true : ".( empty( true ) ? "Yes" : "No" )."<br/>"; // expected return value : 1
echo "declared var : ".( empty( $i ) ? "Yes" : "No" )."<br/>"; // expected return value : 1
echo "non-empty string : ".( empty( "something" ) ? "Yes" : "No" )."<br/>"; // expected return value : 1
echo "non empty array : ".( empty( [10] ) ? "Yes" : "No" )."<br/>"; // expected return 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, ...). Then I propose `is_empty'.

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 could 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 in the next tests involving undefined variables too:

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

Links to any external patches and tests go here.

If there is no patch, make it clear who will create a patch, or whether a volunteer to help with implementation is needed.

Make it clear if the patch is intended to be the final patch, or is just a prototype.

For changes affecting the core language, you should also provide a patch for the language specification.

Implementation

After the project is implemented, this section should contain

  1. the version(s) it was merged into
  2. a link to the git commit(s)
  3. a link to the PHP manual entry for the feature
  4. a link to the language specification section (if any)

References

Links to external references, discussions or RFCs

Rejected Features

Keep this updated with features that were discussed on the mail lists.

rfc/empty_function.1698667913.txt.gz · Last modified: 2023/10/30 12:11 by alessandro.a.rosa_gmail.com