Function to set the default value of an associative array if the key is missing

Is there a function in PHP to set the default value for a variable if it is not set? Some built-in function to replace something like:

$myFruit = isset($_REQUEST['myfruit']) ? $_REQUEST['myfruit'] : "apple" ; 
+6
php
source share
5 answers

In the PHP view, there is an operator for this (starting with 5.3) that would compress your example:

 $myFruit = $_REQUEST['myfruit'] ?: "apple"; 

However , I say β€œview” because it only checks the first operand for false and will not suppress notifications if it is not set. Therefore, if (as in your example) this may not be indicated, your source code is best.

A function similar to the .get dictionary is trivial:

 function dget($dict, $key, $default) { return isset($dict[$key]) ? $dict[$key] : $default; } 

For clarity, I use your original code anyway.

Edit: the implementation of userland # 2 ifsetor () in http://wiki.php.net/rfc/ifsetor is slightly ahead of the function above and works with non-arrays too, but it has the same caveat that the default expression will always be evaluated, even if not in use:

 function ifsetor(&$variable, $default = null) { if (isset($variable)) { $tmp = $variable; } else { $tmp = $default; } return $tmp; } 
+7
source share

As far as I know, there is nothing like this in PHP.

You can implement something like yourself as

 $myVar = "Using a variable as a default value!"; function myFunction($myArgument=null) { if($myArgument===null) $myArgument = $GLOBALS["myVar"]; echo $myArgument; } // Outputs "Hello World!": myFunction("Hello World!"); // Outputs "Using a variable as a default value!": myFunction(); // Outputs the same again: myFunction(null); // Outputs "Changing the variable affects the function!": $myVar = "Changing the variable affects the function!"; myFunction(); 
+1
source share

You can also create a class that implements ArrayAccess , which you pass during the construction of 2 arrays ( $_REQUEST and an array with default values) and select a transparent default value.

Btw., Relying on $_REQUEST , is not a wise idea. See the $_REQUEST manual for more information.

+1
source share

Instead of checking if the key does not exist and then returns the default value, you can also populate your array with these values ​​before accessing it.

 $expectedKeys = array('myfruit'); $requestData = array_merge ( array_combine( $expectedKeys, array_fill(0, count($expectedKeys), null)), $_REQUEST); 

$postData now an array with all the keys you expect (specified by $expectedKeys ), but any entry not in $_REQUEST is null .

 $myFruit = $requestData['myfruit']; if (is_null($myFruit)) { // Value not exists } 

But I also recommend just stopping with the triple operator ?: .

0
source share

There is an ife() function in the CakePHP structure, you can find it here http://api13.cakephp.org/view_source/basics.php/ , this is the last function!

You can use it as follows:

 echo ife($variable, $variable, 'default'); 
-one
source share

All Articles