Note that there are some explanatory texts on larger screens.

plurals
  1. POBest way to test for a variable's existence in PHP; isset() is clearly broken
    primarykey
    data
    text
    <p>From the <a href="http://php.net/isset" rel="noreferrer"><code>isset()</code> docs</a>:</p> <pre><code>isset() will return FALSE if testing a variable that has been set to NULL. </code></pre> <p>Basically, <code>isset()</code> doesn't check for whether the variable is set at all, but whether it's set to anything but <code>NULL</code>.</p> <p>Given that, what's the best way to actually check for the existence of a variable? I tried something like:</p> <pre><code>if(isset($v) || @is_null($v)) </code></pre> <p>(the <code>@</code> is necessary to avoid the warning when <code>$v</code> is not set) but <code>is_null()</code> has a similar problem to <code>isset()</code>: it returns <code>TRUE</code> on unset variables! It also appears that:</p> <pre><code>@($v === NULL) </code></pre> <p>works exactly like <code>@is_null($v)</code>, so that's out, too.</p> <p>How are we supposed to reliably check for the existence of a variable in PHP?</p> <hr> <p>Edit: there is clearly a difference in PHP between variables that are not set, and variables that are set to <code>NULL</code>:</p> <pre><code>&lt;?php $a = array('b' =&gt; NULL); var_dump($a); </code></pre> <p>PHP shows that <code>$a['b']</code> exists, and has a <code>NULL</code> value. If you add:</p> <pre><code>var_dump(isset($a['b'])); var_dump(isset($a['c'])); </code></pre> <p>you can see the ambiguity I'm talking about with the <code>isset()</code> function. Here's the output of all three of these <code>var_dump()s</code>:</p> <pre><code>array(1) { ["b"]=&gt; NULL } bool(false) bool(false) </code></pre> <hr> <p>Further edit: two things.</p> <p>One, a use case. An array being turned into the data of an SQL <code>UPDATE</code> statement, where the array's keys are the table's columns, and the array's values are the values to be applied to each column. Any of the table's columns can hold a <code>NULL</code> value, signified by passing a <code>NULL</code> value in the array. You <em>need</em> a way to differentiate between an array key not existing, and an array's value being set to <code>NULL</code>; that's the difference between not updating the column's value and updating the column's value to <code>NULL</code>.</p> <p>Second, <a href="https://stackoverflow.com/questions/418066/best-way-to-test-for-a-variables-existence-in-php-isset-is-clearly-broken#418162">Zoredache's answer</a>, <code>array_key_exists()</code> works correctly, for my above use case and for any global variables:</p> <pre><code>&lt;?php $a = NULL; var_dump(array_key_exists('a', $GLOBALS)); var_dump(array_key_exists('b', $GLOBALS)); </code></pre> <p>outputs:</p> <pre><code>bool(true) bool(false) </code></pre> <p>Since that properly handles just about everywhere I can see there being any ambiguity between variables that don't exist and variables that are set to <code>NULL</code>, <strong>I'm calling <code>array_key_exists()</code> the official easiest way in PHP to truly check for the existence of a variable</strong>.</p> <p>(Only other case I can think of is for class properties, for which there's <code>property_exists()</code>, which, according to <a href="http://php.net/property_exists" rel="noreferrer">its docs</a>, works similarly to <code>array_key_exists()</code> in that it properly distinguishes between not being set and being set to <code>NULL</code>.)</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

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