DhruvPathak
DhruvPathak

Reputation: 43235

Function to set default value of associative array if the key is not present

Is there a function in PHP to set default value of a variable if it is not set ? Some inbuilt function to replace something like:

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

Upvotes: 7

Views: 6764

Answers (5)

Long Ears
Long Ears

Reputation: 4896

PHP kind of has an operator for this (since 5.3 I think) which would compress your example to:

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

However, I say "kind of" because it only tests if the first operand evaluates to false, and won't suppress notices if it isn't set. So if (as in your example) it might not be set then your original code is best.

The function analogous to dictionary.get is trivial:

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

For clarity, I'd still use your original code.

Edit: The userland implementation #2 of ifsetor() at http://wiki.php.net/rfc/ifsetor is a bit neater than the above function and works with non-arrays too, but has the same caveat that the default expression will always be evaluated even if it's not used:

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

Upvotes: 9

KingCrunch
KingCrunch

Reputation: 131871

Instead of testing, if a key not exists and then return a default value, you can also fill your array with this values, before accessing it.

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

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

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

But I also recommend to just stay with the ternary operator ?:.

Upvotes: 0

tobyS
tobyS

Reputation: 870

You could also create a class implementing the ArrayAccess, which you pass 2 arrays during construction ($_REQUEST and an array with defaults) and make it choose the default value transparently.

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

Upvotes: 1

powtac
powtac

Reputation: 41050

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

You can use it like this:

echo ife($variable, $variable, 'default');

Upvotes: -1

Thariama
Thariama

Reputation: 50832

As far as i know there exists nothing like this in PHP.

You may implement something like this yourself like

$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();

Upvotes: 1

Related Questions