Asked  7 Months ago    Answers:  5   Viewed   38 times

From the isset() docs:

isset() will return FALSE if testing a variable that has been set to NULL.

Basically, isset() doesn't check for whether the variable is set at all, but whether it's set to anything but NULL.

Given that, what's the best way to actually check for the existence of a variable? I tried something like:

if(isset($v) || @is_null($v))

(the @ is necessary to avoid the warning when $v is not set) but is_null() has a similar problem to isset(): it returns TRUE on unset variables! It also appears that:

@($v === NULL)

works exactly like @is_null($v), so that's out, too.

How are we supposed to reliably check for the existence of a variable in PHP?

Edit: there is clearly a difference in PHP between variables that are not set, and variables that are set to NULL:

$a = array('b' => NULL);

PHP shows that $a['b'] exists, and has a NULL value. If you add:


you can see the ambiguity I'm talking about with the isset() function. Here's the output of all three of these var_dump()s:

array(1) {

Further edit: two things.

One, a use case. An array being turned into the data of an SQL UPDATE 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 NULL value, signified by passing a NULL value in the array. You need a way to differentiate between an array key not existing, and an array's value being set to NULL; that's the difference between not updating the column's value and updating the column's value to NULL.

Second, Zoredache's answer, array_key_exists() works correctly, for my above use case and for any global variables:

$a = NULL;
var_dump(array_key_exists('a', $GLOBALS));
var_dump(array_key_exists('b', $GLOBALS));



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 NULL, I'm calling array_key_exists() the official easiest way in PHP to truly check for the existence of a variable.

(Only other case I can think of is for class properties, for which there's property_exists(), which, according to its docs, works similarly to array_key_exists() in that it properly distinguishes between not being set and being set to NULL.)



If the variable you are checking would be in the global scope you could do:

array_key_exists('v', $GLOBALS) 
Wednesday, March 31, 2021
answered 7 Months ago

You are probably expecting PHP to use the $_POST and $_GET as global variables. PHP used to be setup this way, back in the day, but newer versions require you to explicitly reference these variables.

You could try this:

setdefault($_GET['id'], 0);

function setdefault(&$var, $default="") 
   if (!isset($var)) 
      $var = $default;

or even more simply (using the ternary operator):

$id = array_key_exists('id', $_GET) ? $_GET['id'] : 0;
Wednesday, March 31, 2021
answered 7 Months ago

isset determine if a variable is set and is not NULL. $_POST will always be set and will always be an array.

Without isset you are just testing if the value is truthy. An empty array (which $_POST will be if you aren't posting any data) will not be truthy.

Saturday, May 29, 2021
answered 5 Months ago

You could also try EXISTS:


and per the documentation, you can SELECT anything.

Traditionally, an EXISTS subquery starts with SELECT *, but it could begin with SELECT 5 or SELECT column1 or anything at all. MySQL ignores the SELECT list in such a subquery, so it makes no difference.

Tuesday, June 1, 2021
answered 5 Months ago

As @Martin says, REST implemented properly is independent of the client (assuming it supports your representation types). So for example you can use the Poster add-on for Firefox, or RestClient 2.3.

Friday, August 13, 2021
answered 2 Months ago
Only authorized users can answer the question. Please sign in first, or register a free account.
Not the answer you're looking for? Browse other questions tagged :