All,

I'm sure this has been asked somewhere, but since I see requests for features for 5.2 or 6.0, I'd like to add a "simple" item to the list which would be quite useful to me and would simplify and clean up a lot of code out there:

   function coalesce(...)

This works much like in the SQL version of the same. In SQL, the function returns the first non-null argument from an arbitrary list. In our use, it should return the first non-empty value from the list:

Example:

   $x = "dante";
   $y = "";
   $z = "";

   $value = coalesce($z, $y, $x); // $value = "dante"

This function would ideally be built into the language and bypass warnings about undefined variables like 'empty' does. It might be nice to have several varieties of this function:

   * return first parameter where empty() is FALSE
   * return first parameter where isset() is TRUE

I don't think something like this can NOT be written in userspace because the 'isset' and 'empty' checks need to be run before arguments can be passed to a user function or warnings will start flying. A function like this simplifies code which used to look like this:

   if (!empty($_POST["postkey"])) {
       $value = $_POST["postkey"];
   }
   elseif (!empty($_GET["getkey"])) {
       $value = $_POST["getkey"];
   }
   elseif (!empty($default_value)) {
       $value = $default_value;
   }
   else {
       $value = "hard coded value";
   }

Into this:

$value = coalesce($_POST["postkey"], $_GET["getkey"], $default_value, "hard coded value");

Can this be built and included?

Dante

--
PHP Internals - PHP Runtime Development Mailing List
To unsubscribe, visit: http://www.php.net/unsub.php

Reply via email to