rfc:empty_function

Differences

This shows you the differences between two versions of the page.

Link to this comparison view

Both sides previous revisionPrevious revision
Next revision
Previous revision
rfc:empty_function [2023/10/31 10:03] – Under Discussion alessandro.a.rosa_gmail.comrfc:empty_function [2023/10/31 16:42] (current) – Under Discussion alessandro.a.rosa_gmail.com
Line 7: Line 7:
  
 ===== Introduction ===== ===== Introduction =====
-The official documentation of the function empty() is at https://www.php.net/manual/en/function.empty.php. All the quotations below have been reported from the online version up to the date of this RFC, namely October 31th 2023.+The official documentation of the function <php>empty()</php> is at https://www.php.net/manual/en/function.empty.php. All the quotations below have been reported from the online version up to the date of this RFC, namely October 31th 2023.
  
-Here the purpose of the function empty() is to `//Determine whether a variable is considered to be empty//'. Before exploring the behavior of this built-in function, we would argue upon the following sentence: `//A variable is considered empty if it does not exist or if its value equals false//'. While we agree on the first part, we could not do about the second half, as it throws an exception about the meaning of `//emptiness//'.+Here the purpose of the function <php>empty()</php> is to `//Determine whether a variable is considered to be empty//'. Before exploring the behavior of this built-in function, we would argue upon the following sentence: `//A variable is considered empty if it does not exist or if its value equals false//'. While we agree on the first part, we could not do about the second half, as it throws an exception about the meaning of `//emptiness//'.
  
-Defining `emptiness' might be very subtle and actually not all the most blasoned online dictionaries succeed in giving a quite insightful definition. First,+Defining `emptiness' might be very subtle argument and actually not all the most blasoned online dictionaries succeed in giving a quite insightful definition. First,
 > Merriam-Webster: //`containing nothing', `not occupied or inhabited'// > Merriam-Webster: //`containing nothing', `not occupied or inhabited'//
  
Line 22: Line 22:
 > Dictionary.com: `//the fact or state of containing nothing or of being without the usual or appropriate contents//'. > Dictionary.com: `//the fact or state of containing nothing or of being without the usual or appropriate contents//'.
  
-Before our conclusions, I will first resume basic notions about variable declaration; each example is followed by some equivalent statement in C-like languages, as they are pretty clearer about the strict connection between the kind of containers and contents.+Before our conclusions, I will first resume basic notions about variable declaration; each example is followed by some equivalent statement in C-family languages, as they are pretty clearer about the strict connection between the kind of containers and contents.
  
-<code>+<PHP>
 //a container for storing an integer number was instantiated, //a container for storing an integer number was instantiated,
 //named `integer' and set to the constant value 1 //named `integer' and set to the constant value 1
 $integer = 1; $integer = 1;
  
-//In C-like languages, it will ruled through this instantiation+//In C-family languages, it will ruled through this instantiation
 //int <var_name> = 1; //int <var_name> = 1;
-</code>+</PHP>
  
 Emptiness here occurs __only__ when Emptiness here occurs __only__ when
-<code>$integer;</code>+<PHP>$integer;</PHP>
 is coded. is coded.
  
-<code>+<PHP>
 //a container for storing an integer number was instantiated, //a container for storing an integer number was instantiated,
 //named `float' and set to the constant value 1.1 //named `float' and set to the constant value 1.1
 $float = 1.1; $float = 1.1;
  
-//In C-like languages, it will ruled through this instantiation+//In C-family languages, it will ruled through this instantiation
 //double <var_name> = 1.1; //double <var_name> = 1.1;
-</code>+</PHP>
  
 Analogously, emptiness here occurs __only__ when Analogously, emptiness here occurs __only__ when
-<code>$float;</code+<PHP>$float;</PHP
-is coded.+is coded. A slightly variegated situation occurs for emptiness of strings:
  
-<code>+<PHP>
 //a container for storing a sequence of characters was instantiated //a container for storing a sequence of characters was instantiated
 //named `string' and then set to something //named `string' and then set to something
 $string = "<any or no character>"; $string = "<any or no character>";
  
-//In C-like languages, it corresponds to the pointer-like syntax+//In C-family languages, it corresponds to the pointer-like syntax
 //char* <var_name> = "<any or no characters>"; //char* <var_name> = "<any or no characters>";
-</code>+</PHP>
  
 Emptiness here occurs in two cases: when Emptiness here occurs in two cases: when
-<code>$string;</code>+<PHP>$string;</PHP>
 or or
-<code>$string="";</code>+<PHP>$string="";</PHP>
 are coded. are coded.
  
-The definition of `emptiness' changes according to the kind of container. Therefore the second option in the official definition of emptiness in the empty function documentation, namely `//a variable is considered empty if its value equals false//', cannot hold logically holds, with regard to the essence of the variable container. The constant boolean value `false' alludes to the instantiation of a one-byte container, which is filled through the `false' value, hence the one-byte container not be judged as `empty'. Moreover, the same objection holds for the zero (0) input value, according to the official return value of empty() function in php.net, being documented as follows:+We conclude that the definition of `emptiness' changes according to the kind of container. Therefore the second option in the official definition of emptiness in the empty function documentation, namely `//a variable is considered empty if its value equals false//', cannot logically holds, with regard to the essence of the variable container. The constant boolean value `''false''' alludes to the instantiation of a one-byte container, which is filled through the `''false''' constant value, hence the one-byte container cannot be judged as `empty'. Moreover, the same objection holds for the zero (0) input value, according to the official return value of <php>empty()</php> function in php.net, being documented as follows:
 `//Returns true if var does not exist or has a value that is empty or equal to zero, aka falsey, see conversion to boolean. Otherwise returns false//'. `//Returns true if var does not exist or has a value that is empty or equal to zero, aka falsey, see conversion to boolean. Otherwise returns false//'.
  
-In my viewpoint, the optimal behavior of any emptiness-test function shall rely upon the match between the container and the kind of contents. Resuming, there are the flaws that should be eventually settled:+In my viewpoint, the optimal behavior of any emptiness-test function shall rely upon the match between the container and the kind of contents. I resumed here below the flaws that should be eventually settled:
  
-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.+1) the **semantics**: the function name is ambiguous about the action, that is, it is not clear whether the goal of <php>empty()</php> function should consist of testing the `emptiness' of input variable or return an empty version of the input variable.
  
 2) the **behavior**. The only three issues arise from inputing no variable or managing the false and 0 constant values. 2) the **behavior**. The only three issues arise from inputing no variable or managing the false and 0 constant values.
Line 76: Line 76:
 The ordinary behavior with all the built-in datatypes has been listed below: The ordinary behavior with all the built-in datatypes has been listed below:
  
-<code>+<PHP>
 echo "built-in function `empty' behavior:\n"; echo "built-in function `empty' behavior:\n";
 echo "Stage 1: empty, undeclared or null containers (`empty' function)\n"; echo "Stage 1: empty, undeclared or null containers (`empty' function)\n";
Line 103: Line 103:
 echo "non-empty string >> empty( \"something\" ) : ".( empty( "something" ) ? "Empty" : "Not empty" )."\n"; echo "non-empty string >> empty( \"something\" ) : ".( empty( "something" ) ? "Empty" : "Not empty" )."\n";
 echo "non empty array >> empty( [10] ) : ".( empty( [10] ) ? "Empty" : "Not empty" )."\n"; echo "non empty array >> empty( [10] ) : ".( empty( [10] ) ? "Empty" : "Not empty" )."\n";
-</code>+</PHP>
  
-We remark, to the benefit of readers, that the empty string is namely "", that is, no characters between (double)quotes. The empty space, namely " ", cannot match the definition of emptiness. Contents of string containers are properly characters only (i.e., any kind of symbol); hence, according to the dictionaries definitions listed at the top, the emptiness of string occurs when the given variable container for string storage includes no character in general, either if the statements+We remark, to the benefit of readers, that the empty string is namely ''""'', that is, no characters between (double)quotes. The empty space, namely ''" "'', cannot match the definition of emptiness. Contents of string containers are properly characters only (i.e., any kind of symbol); hence, according to the dictionaries definitions listed at the top of this RFC, the emptiness of some string occurs when the given variable container for string storage includes no character in general, either if the statements
  
-<code>$string;</code>+<php>$string;</php>
  
 or  or 
  
-<code>$string="";</code>+<php>$string = "";</php>
  
 are coded. are coded.
Line 118: Line 118:
 I understand that this proposal could represent a huge break, anyway I believe that my points above shall not be disregarded in order to strengthen parts of PHP that look flawed or fuzzy so far. Honestly, I still read tons of people out there mocking up PHP for its non-typified nature. Also consider that 1) __semantics__, as you know, is essential for languages. 2) the function __behavior__ match the purposes declared in its name as well as it must cover all cases as possible, without exceptions. I understand that this proposal could represent a huge break, anyway I believe that my points above shall not be disregarded in order to strengthen parts of PHP that look flawed or fuzzy so far. Honestly, I still read tons of people out there mocking up PHP for its non-typified nature. Also consider that 1) __semantics__, as you know, is essential for languages. 2) the function __behavior__ match the purposes declared in its name as well as it must cover all cases as possible, without exceptions.
  
-First, the `emptyname 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.+First, the <php>empty()</php> name shall be adequated to the <php>is_*()</php> family of php built-in functions (<php>is_null()</php><php>is_bool()</php><php>is_string()</php><php>is_array()</php>, ...). Hence I propose <php>is_empty()</php>. 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 all the above flaws: Second, I implemented the following version which fixes all the above flaws:
  
-<code>+<PHP>
 function is_empty( $input = null ) function is_empty( $input = null )
 { {
Line 130: Line 130:
      return preg_match( "/0\:(\{\}|\[\]|\"\")\;?$/i", $ser ) === 1 ? 1 : 0;      return preg_match( "/0\:(\{\}|\[\]|\"\")\;?$/i", $ser ) === 1 ? 1 : 0;
 } }
-</code>+</PHP>
  
-Let me first say that I resorted to the `serializebuilt-in function just for showing here how the function is_empty() should work. The serialization just represents an escamotage in PHP for obtaining a formal basis for eventually checking the input variable emptiness in the examples below. However, I presume that the internal C-version would approach emptiness test in most proper ways involving memory tests. Please, note that I suppressed warnings in order to let this function run throughout the next tests, that also cover undefined variables:+Let me first say that I resorted to the <php>serialize()</php> built-in function just for showing here how the function <php>is_empty()</php> should work. The serialization just represents an escamotage in PHP for obtaining a formal basis for eventually checking the input variable emptiness in the examples below. However, I presume that the internal C-implementation would approach emptiness in most proper ways involving memory tests. Please, note that I suppressed warnings in order to let this function run throughout the next tests, that also cover undefined variables:
  
-<code>+<PHP>
 echo ">new `is_empty()' function behavior\n"; echo ">new `is_empty()' function behavior\n";
 echo "Stage 1: empty, undeclared or null containers (`is_empty' function)\n"; echo "Stage 1: empty, undeclared or null containers (`is_empty' function)\n";
Line 161: Line 161:
 echo "resource >> is_empty( \$res ) : ".( is_empty( $res ) ? "Empty" : "Not empty" )."\n"; echo "resource >> is_empty( \$res ) : ".( is_empty( $res ) ? "Empty" : "Not empty" )."\n";
 if ( $res !== false ) fclose( $res ); if ( $res !== false ) fclose( $res );
-</code>+</PHP>
  
 The above code works correctly covers all cases of basic datatypes and, last but not least, can be also finely read. The above code works correctly covers all cases of basic datatypes and, last but not least, can be also finely read.
Line 167: Line 167:
 ===== Proposed PHP Version(s) ===== ===== Proposed PHP Version(s) =====
 According to the official policies documented at https://wiki.php.net/rfc/releaseprocess, backwards compatibility can only be broken According to the official policies documented at https://wiki.php.net/rfc/releaseprocess, backwards compatibility can only be broken
-in a major version, hence the new function is_empty() would appear not before than version 9.0+in a major version, hence the new function <php>is_empty()</php> would appear not before than version 9.0 
 + 
 +The option of keeping <php>empty()</php> function together with ''is_empty()'' is fine too, so deprecation is not required: backward compatibility would be saved, though I would recommend the usage of ''is_empty()'' however.
  
 ===== RFC Impact ===== ===== RFC Impact =====
rfc/empty_function.1698746618.txt.gz · Last modified: 2023/10/31 10:03 by alessandro.a.rosa_gmail.com