rfc:empty_function

This is an old revision of the document!


PHP RFC: standard built-in is_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

We remark, to the benefit of any reader, that the empty string is “”, that is, nothing between (double)quotes. The empty space, such as “ ”, cannot be ascribed to emptiness definition.

Proposal

I understand that this proposal could represent a huge break, anyway I believe that my two points above shall not be disregarded in order to strengthen parts of PHP that look flawed or fuzzy so far. 1) Semantics, as you know, is essential for languages. 2) The function behavior must cover all cases as possible, without exceptions.

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'. Because of the importance of this break, I recommend the new is_empty() function to co-live together with the older one empty() until the latter will be definitely superseeded after 2 or 3 versions (suggest deadline) from the official embedding in the standard library. Assuming empty() to be deprecated since the version 8.x, it will be removed after version 8.(x+3) or 8.(x+4), that would take 3 or 4 years, as I presume.

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.

rfc/empty_function.1698681250.txt.gz · Last modified: 2023/10/30 15:54 by alessandro.a.rosa_gmail.com