Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p><a href="http://au2.php.net/isset" rel="nofollow noreferrer"><code>isset()</code></a> is the usual way of doing this:</p> <pre><code>if (!isset($blah)) { $blah = 'foo'; } </code></pre> <p><strong>Note:</strong> you can assign <code>null</code> to a variable and it will be assigned. This will yield different results with <code>isset()</code> and <a href="http://au2.php.net/manual/en/function.is-null.php" rel="nofollow noreferrer"><code>is_null()</code></a> so you need to be clear on what you mean by "not assigned". See <a href="http://brian.moonspot.net/null-isset-php" rel="nofollow noreferrer">Null vs. isset()</a>. This is also one case where you need to be careful about doing automatic type conversion, meaning using the <code>!=/==</code> or <code>===/!==</code> depending on the desired result.</p> <p>You can use boolean shorthand for this too (which is what the Perl <code>||=</code> operator is). As of PHP 5.2.x there is no operator like you speak of. In Perl:</p> <pre><code>$a ||= $b; </code></pre> <p>is equivalent to:</p> <pre><code>$a = $a || $b; </code></pre> <p>You can do the second form in PHP but PHP has some funky rules about <a href="http://us.php.net/manual/en/language.types.type-juggling.php" rel="nofollow noreferrer">type juggling</a>. See <a href="http://us.php.net/manual/en/language.types.boolean.php#language.types.boolean.casting" rel="nofollow noreferrer">Converting to boolean</a>:</p> <blockquote> <p>When converting to <a href="http://us.php.net/manual/en/language.types.boolean.php" rel="nofollow noreferrer">boolean</a>, the following values are considered FALSE:</p> <ul> <li>the <a href="http://us.php.net/manual/en/language.types.boolean.php" rel="nofollow noreferrer">boolean</a> <code>FALSE</code> itself</li> <li>the <a href="http://us.php.net/manual/en/language.types.integer.php" rel="nofollow noreferrer">integer</a> 0 (zero)</li> <li>the <a href="http://us.php.net/manual/en/language.types.float.php" rel="nofollow noreferrer">float</a> 0.0 (zero)</li> <li>the empty <a href="http://us.php.net/manual/en/language.types.string.php" rel="nofollow noreferrer">string</a>, and the <a href="http://us.php.net/manual/en/language.types.string.php" rel="nofollow noreferrer">string</a> "0"</li> <li>an <a href="http://us.php.net/manual/en/language.types.array.php" rel="nofollow noreferrer">array</a> with zero elements</li> <li>an <a href="http://us.php.net/manual/en/language.types.object.php" rel="nofollow noreferrer">object</a> with zero member variables (PHP 4 only)</li> <li>the special type <a href="http://us.php.net/manual/en/language.types.null.php" rel="nofollow noreferrer"><code>NULL</code></a> (including unset variables)</li> <li><a href="http://us.php.net/manual/en/ref.simplexml.php" rel="nofollow noreferrer">SimpleXML</a> objects created from empty tags</li> </ul> <p>Every other value is considered TRUE (including any resource).</p> </blockquote> <p>So after:</p> <pre><code>$a = 0; $a = $a || 5; </code></pre> <p>$a would equal 5. Likewise:</p> <pre><code>$a = 0; $b = ''; $c = $a == $b; // true $d = $a === $b; // false </code></pre> <p>You have to watch out for these things.</p>
 

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