Вопрос:

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

php isset

31096 просмотра

6 ответа

351 Репутация автора

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?

Автор: Vitalynx Источник Размещён: 14.12.2013 11:56

Ответы (6)


0 плюса

24 Репутация автора

isset — Determine if a variable is set and is not NULL.

!empty — Determine whether a variable is NOT empty.

Автор: user3102083 Размещён: 14.12.2013 11:58

47 плюса

10662 Репутация автора

Решение

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.

For more information, see this article

Автор: Nambi Размещён: 14.12.2013 11:59

4 плюса

1136 Репутация автора

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

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

Автор: Prashant16 Размещён: 14.12.2013 12:03

0 плюса

110 Репутация автора

Isset return false if variable has not been set or it is null and return true if variable has been set and not null.

!empty return true if variable has been set and not empty. Empty string, empty array, "0",0 and false are defined as empty.

Автор: joy Размещён: 14.12.2013 01:14

16 плюса

4455 Репутация автора

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

Автор: Black Mamba Размещён: 24.02.2017 06:12

0 плюса

49 Репутация автора

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)
Автор: Andrey P. Размещён: 05.07.2017 10:04
Вопросы из категории :
32x32