Vitalynx
Vitalynx

Reputation: 984

What's the difference between 'isset()' and '!empty()' in PHP?

I don't understand the difference between isset() and !empty().

Because if a variable has been set, isn't it the same as not being empty?

Upvotes: 53

Views: 54425

Answers (4)

Nambi
Nambi

Reputation: 12042

ISSET checks the variable to see if it has been set. In other words, it checks to see if the variable is any value except NULL or not assigned a value. ISSET returns TRUE if the variable exists and has a value other than NULL. That means variables assigned a "", 0, "0", or FALSE are set, and therefore are TRUE for ISSET.

EMPTY checks to see if a variable is empty. Empty is interpreted as: "" (an empty string), 0 (integer), 0.0 (float)`, "0" (string), NULL, FALSE, array() (an empty array), and "$var;" (a variable declared, but without a value in a class.

Upvotes: 86

Andrey P.
Andrey P.

Reputation: 147

And one more remark. empty() checks if the variable exists as well. I.e. if we perform empty() to the variable that wasn't declared, we don't receive an error, empty() returns 'true'. Therefore we may avoid isset() if next we need to check if the variable empty.

So

isset($var) && !empty($var)

will be equals to

!empty($var)

Upvotes: 4

Black Mamba
Black Mamba

Reputation: 15545

Source :http://php.net/manual/en/types.comparisons.phpThis page shows the comparison of the empty(),is_null(),isset().

The picture showing complete comparison here

Upvotes: 34

Prashant16
Prashant16

Reputation: 1526

The type comparison tables gives answer of all question about these operators

http://php.net/manual/en/types.comparisons.php

Upvotes: 5

Related Questions