Note that there are some explanatory texts on larger screens.

plurals
  1. POIsset()/Empty(), Arrays and Ternary Operator
    text
    copied!<p>When you need to check is there any value in array, and if not - set default value for variable, you do something like this</p> <pre><code>$authToken = isset($response['fbData']['authResponse']['accessToken']) ? $response['fbData']['authResponse']['accessToken'] : null; </code></pre> <p>I wonder, is there any way to do it somehow more readable?</p> <p>There is short version of ternary operator</p> <pre><code>$authToken = $sourceVar ?: null; </code></pre> <p>No need to repeat source here second time, but it doesn't work with arrays, because if I will use <code>$response['fbData']['authResponse']['accessToken']</code> instead of <code>$sourceVar</code> - I will get php warning message.</p> <p>I can use <code>@</code></p> <pre><code>$authToken = @$response['fbData']['authResponse']['accessToken'] ?: null; </code></pre> <p>I heard that they made "@" really fast in <code>PHP 5.4</code>, but I don't like this anyway.</p> <p>I could use something like this</p> <pre><code>$a = isset($response['fbData']['authResponse']['accessToken']) ?: null; </code></pre> <p>But in this case I will get TRUE as result, result of <code>isset()</code>.</p> <p>So, maybe somebody knows, how to do it right?</p> <p><strong>Upd1:</strong> Suggested topic <a href="https://stackoverflow.com/questions/6347694/how-can-i-create-a-new-operator-in-php">How can I create a new operator in PHP?</a> 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 <code>$response['fbData']['authResponse']['accessToken']</code> in function because it will generate warning message).</p> <p><strong>Upd2: Problem solved</strong>, 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:</p> <pre><code>function myIsset (&amp;$var, $defaultVal = null) { return isset($var) ? $var : $defaultVal; } $authToken = myIsset($response['fbData']['authResponse']['accessToken']); </code></pre> <p><strong>UPD2 10/27/2015:</strong> PHP 7 has new awesome operator <code>??</code> (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.</p> <pre><code>$authToken = $response['fbData']['authResponse']['accessToken'] ?? 'any_default_value'; </code></pre> <p>And if requested array key does not exits - it does not generate <code>php notice</code>!</p> <p>You can use it also like this, looking for not empty value:</p> <pre><code>$username = $_GET['user'] ?? $_POST['user'] ?? 'nobody'; </code></pre>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload