6

When you need to check is there any value in array, and if not - set default value for variable, you do something like this

$authToken = isset($response['fbData']['authResponse']['accessToken']) ? $response['fbData']['authResponse']['accessToken'] : null;

I wonder, is there any way to do it somehow more readable?

There is short version of ternary operator

$authToken = $sourceVar ?: null;

No need to repeat source here second time, but it doesn't work with arrays, because if I will use $response['fbData']['authResponse']['accessToken'] instead of $sourceVar - I will get php warning message.

I can use @

$authToken = @$response['fbData']['authResponse']['accessToken'] ?: null;

I heard that they made "@" really fast in PHP 5.4, but I don't like this anyway.

I could use something like this

$a = isset($response['fbData']['authResponse']['accessToken']) ?: null;

But in this case I will get TRUE as result, result of isset().

So, maybe somebody knows, how to do it right?

Upd1: Suggested topic How can I create a new operator in PHP? is not answers my question because there user asks "how to change PHP" and create a new operator. I do not need this. There are some answers how to do similar things without PHP modifications but they are do not work for arrays (I can't pass something like this $response['fbData']['authResponse']['accessToken'] in function because it will generate warning message).

Upd2: Problem solved, thanks to NikiC (see comment below). The idea: Php wont generate warning message if I will pass array element as a link, so it gives us a chance to use something like this:

function myIsset (&$var, $defaultVal = null) {
    return isset($var) ? $var : $defaultVal;
}

$authToken = myIsset($response['fbData']['authResponse']['accessToken']);

UPD2 10/27/2015: PHP 7 has new awesome operator ?? (The null coalesce operator) that solved this problem completely and is the best way to do it: It returns its first operand if it exists and is not NULL; otherwise it returns its second operand.

$authToken = $response['fbData']['authResponse']['accessToken'] ?? 'any_default_value';

And if requested array key does not exits - it does not generate php notice!

You can use it also like this, looking for not empty value:

$username = $_GET['user'] ?? $_POST['user'] ?? 'nobody';
Community
  • 1
  • 1
Pavel
  • 3,967
  • 2
  • 29
  • 35
  • 2
    See here for one way to make this more pleasant: http://stackoverflow.com/questions/5836515/what-is-the-php-shorthand-for-print-var-if-var-exist/5836648#5836648 – NikiC Apr 09 '13 at 17:25
  • 1
    Wow, this is an answer! :) I didn't know, that if you pass not defined variable or array with not defined keys to function as link - php doesn't generate warning message. This is really good solution for me! Thank you! – Pavel Apr 09 '13 at 18:38
  • 1
    The process of passing a variable to a function by references creates the variable if it doesn't exist. You can see similar behavior in `preg_match()` and similar functions. – Sam Dufel Apr 09 '13 at 21:48

0 Answers0