1

I want to know what is different between isset() and !empty?

I know that isset() tests if a variable is set and not null, while empty() can return true when the variable is set to certain values.

But logically when isset() which doesn't empty (without using "empty()"), there is a variable in text or textarea whatever so we don't need empty().

Shadow The GPT Wizard
  • 66,030
  • 26
  • 140
  • 208
Amr Magdy
  • 43
  • 1
  • 8
  • 2
    foo = "" will make isset() return true, but !empty will return false. The variable foo is set to nothing, but it is still set. It has no content so !empty will return false. – Kevin Feb 02 '15 at 15:29
  • I would also point you to the PHP documentation for these functions. They should be pretty clear around how different variable type/values respond to each function. – Mike Brant Feb 02 '15 at 15:33
  • this code is quite tricky ` $a = 0; if(empty($a)){ echo"EMPTY"; } if(isset($a)){ echo"EMPTY"; } ?>` Variable a is set but it is empty in a way that empty sees. You may test such thing with a regex "^$" (empty). – Szymon Roziewski Feb 02 '15 at 15:40

2 Answers2

2

isset method checks if a variable exists or not. On the other hand !empty knows that a variable exists but it needs to check its value.

Linesofcode
  • 5,327
  • 13
  • 62
  • 116
1

The difference is quite small, but significant enough to not make mistakes with both statements. When you declare a variable like this:

<?php
    foo = "";
?>

You will end up with different values depending on what you use. isset() will return true, as the variable foo was set to nothing. However !empty will return false, as the variable foo does not contain anything. Basically isset() only checks for NULL values, where !empty checks for for everything that is considered 0 (so NULL, 0, 0.0 etc. but also 0 as a string, for example).

Kevin
  • 874
  • 3
  • 16
  • 34