isset() vs empty()

isset() usage

Example 1:


$post['var'] = 100;

isset($post[‘var’]) will return true

Example 2:

isset($name) will return false if $name has not been declared

Example 3:

$post['v1'] = 100;

isset($post[‘v2’]) will return false

since isset is not assigned

empty()

It essentially means

!isset($var) || $var == false.

So it is either is not set or it is set but its value is false

Example 1


$post['v1'] = 100;

empty($post[‘v1’]) is false

Example 2


$post['v2'] = false

empty($post[‘v2’])  is true

Example 3

empty($post[‘v3’]) is true because $post[‘v3] has not been declared

Leave a Reply

Your email address will not be published. Required fields are marked *