PHP: Test the value of a variable using isset() vs empty() vs is_null()


Test the value of a variable using  isset() vs empty() vs is_null():

	$xx = '';
	$yy = [];
	$zz = null;

	echo "
<h2> is_null()</h2>
";

	var_dump(is_null($xx)); // False

	var_dump(is_null($yy)); // False

	var_dump(is_null($zz)); // True

	echo "
<h2> empty()</h2>
";
	var_dump(empty($xx)); // True

	var_dump(empty($yy)); // True

	var_dump(empty($zz)); // True

	echo "
<h2> isset()</h2>
";

	var_dump(isset($xx)); // True
	var_dump(isset($yy)); // True
	var_dump(isset($zz)); // False

Result of code

is_null()

bool(false) bool(false) bool(true)

empty()

bool(true) bool(true) bool(true)

isset()

bool(true) bool(true) bool(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