17. isset and empty and Null

isset and empty are not actually regular functions but language constructs. That means they’re part of the PHP language itself.
bool isset( mixed $var [, mixed … ])
Returns true if the variable exists and is not null. Does not trigger an error if the variable does not exist.

<?php
	$foo = 'bar';
	var_dump(isset($foo));        //true

	$baz = null;
	var_dump(isset($baz));        //false

	var_dump(isset($undefined));  //false
?>
if ($_GET['var'] == 'foo')

If $_GET[‘var’] actually exists, that is, if the URL contains ?var=foo, this will work just fine, regardless of the value of var. The presence or absence of $_GET[‘var’] is entirely in the hand of the user though. If the user does not include ?var=foo in the URL, this program will trigger an error.

To avoid this, we need to use isset:

if (isset($_GET['var']) && $_GET['var'] == 'foo')

isset can accept multiple arguments and only returns true if all of them are set:

if (isset($foo, $_GET['bar'], $array['baz'])) {
    // all needed values exist, do something with them
}

bool empty( mixed $var )
Returns true if the variable does not exist or its value equals false in a loose comparison. Does not trigger an error if the variable does not exist.

<?php
	$foo = 'bar';
	var_dump(empty($foo));        // false

	$baz = null;
	var_dump(empty($baz));        // true

	var_dump(empty($undefined));  // true
?>

The important and remarkable thing about this behavior is that when trying to pass non-existent variables to normal functions, an error is triggered. PHP first tries to get the value of the variable, then pass it into the function:

  var_dump($foobar);   -> Notice: Undefined variable: foobar NULL

Null:
When talking about isset one inevitably must also talk about null. Let’s come back for a second to the predicament of PHP trying to skip over trivial errors without crashing a program.

<?php
	$foo = $_GET['var'];
	$bar = 'bar';
	if ($foo == $bar) {
		// do something
	}
?>

How should PHP behave in the above program if $_GET[‘var’] did not exist? Sure, it will throw a tantrum and trigger a notice, and that’s good and fine. But how should it treat $foo afterwards? As some sort of special outcast that doesn’t have a value? Should it trigger an error every time $foo is used hence? The answer is simple: PHP assigns the value null in place of the non-existent variable. null is a type unto its own. null is not a boolean, not an integer, not a string, not an object. null is of type null which can only have one value: null. null is used to mean the absence of a value, but null is just a regular value in itself. null loosely compares to false (null == false, but null !== false).

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s