13

I am new to the concept of empty and null. Whilst I have endeavoured to understand the difference between them, I am more confused. I came across an article at http://www.tutorialarena.com/blog/php-isset-vs-empty.php however I still don't see when you would use isset and empty when validating forms. Seeing that I don't grasp the difference, I don't want to be using the incorrect functions as well as not be able to use the functions in other areas. Can someone give examples that will help me understand? I am very new to coding so would appreciate if someone could give me real world examples and at the same time keep it simply enough for noob to follow.

PeanutsMonkey
  • 6,919
  • 23
  • 73
  • 103
  • possible duplicate of http://stackoverflow.com/questions/669488/detecting-insufficient-php-variables-false-vs-null-vs-unset-vs-empty – nathan gonzalez Apr 11 '11 at 00:41
  • possible duplicate of [what difference between NULL, blank and empty in php?](http://stackoverflow.com/questions/2461239/what-difference-between-null-blank-and-empty-in-php) – Gordon Apr 11 '11 at 07:13

9 Answers9

20

A variable is NULL if it has no value, and points to nowhere in memory.

empty() is more a literal meaning of empty, e.g. the string "" is empty, but is not NULL.

The following things are considered to be empty:

  • "" (an empty string)
  • 0 (0 as an integer)
  • 0.0 (0 as a float)
  • "0" (0 as a string)
  • NULL
  • FALSE
  • array() (an empty array)
  • var $var; (a variable declared, but without a value in a class)

Source.


Example

$a is NULL.

$a = '' is empty, but not NULL.


Update

If $a='' is empty but not NULL, when do I use the empty() function and when do I use the isset() function.

isset() will return FALSE is the variable is pointing to NULL.

Use empty() when you understand what is empty (look at the list above).

Also when you say it points nowhere in memory, what does that mean exactly?

It means that $str = '' will be in memory as a string with length of 0.

If it were $str = NULL, it would not occupy any memory.

alex
  • 479,566
  • 201
  • 878
  • 984
  • Thanks Alex. Now the confusing part. If $a='' is empty but not NULL, when do I use the empty() function and when do I use the isset() function. Aslo when you say it points nowhere in memory, what does that mean exactly? – PeanutsMonkey Apr 11 '11 at 00:51
  • Thanks @Alex. That just confused me more. So let me give you an example and maybe you can help me clear my confusion. I have a form where I expect users to provide their username and password. Now I want to validate if the user has posted the form as well as if the user has entered a username and password. Now in that example when would I consider using isset and when would I consider using empty and why? – PeanutsMonkey Apr 11 '11 at 01:03
  • @PeanutsMonkey I would definitely use `empty()` in that situation. You should only ever receive strings from a user submitted form. It may be wise to `trim()` the value first, otherwise a string consisting of whitespace will not be empty. – alex Apr 11 '11 at 01:06
  • @Alex. Thanks. Okay now if expand on that example so that I actually understand when to use isset and empty. I understand that every value with the exception of the following would equal true for the function empty(). This is $value='a'. This would mean that empty is false. Now if there is any other value or no value i.e. $a; $a=''; $a=0; $a='0'; $a=null; $a=false; then empty() would equal true. When empty is false e.g. $a='a';, then a variable has been set. – PeanutsMonkey Apr 11 '11 at 01:17
  • @Alex continued - Why would I ever use isset to validate data when empty() is much broader because my understanding is that empty() caters for $a='' which would be that no data was entered by the user which would be mean that the variable has been set and it is NOT null? – PeanutsMonkey Apr 11 '11 at 01:18
  • @Alex - Because as I understand if I were to use empty(), it would have to be written as such if(empty($_POST["submit"])) { } which would mean that if the form has been submitted and has a value of submit. If i wanted to cater to validate if the user actually entered any data, I would do as follows; if(empty($_POST["submit"])) { if(empty($_POST["username"])) print $username; else print "Please enter a username"; } } – PeanutsMonkey Apr 11 '11 at 01:22
  • @Alex - When you say memory, do you mean within the web server's memory? – PeanutsMonkey Apr 11 '11 at 01:25
  • @PeanutsMonkey Yes, the server's memory (the amount allocated to your PHP process). – alex Apr 11 '11 at 01:35
  • @Alex - Correct me if I am wrong $a=1; if(!empty($a)) { print "This is not an empty value"; } is the correct way to confirm that $a is not true for the function empty() – PeanutsMonkey Apr 11 '11 at 01:39
  • @PeanutsMonkey Yes, to flip the condition, use the negation operator `!`. Then it reads if *if **not** empty*... – alex Apr 11 '11 at 01:44
8

Null is a placeholder that generally means "no data about this is available".

The use of null for this is just a convention, but a rather widespread one, to the point where some programming languages support the convention directly. The reason this convention exists has IMHO historically to do with "pointers"; many times a procedure will be defined to return a pointer to an answer, and will return what is traditionally called a Null pointer if it could not produce an answer for some reason.

Empty means (if this is a set) that it has no members. That's an explicit answer, and it is very different than "no data about this is available".

In the PHP world, apparantly uninitialized variables have the Null value, and isset on such a variable returns FALSE. For arrays and strings, PHP follows the convention that "empty" means "has no members" although arrays and strings are not technically sets.

PHP apparantly has this funny idea that 0 and 0.0 are also "empty", by PHP design. That's abusive of the concept of "empty" IMHO: Individual numbers are not sets, so 0 can't reasonably by "empty". THis just leads to obscure programming because it violates the principle of least surprise. I'm sure the PHP designers would are that "zero is the empty number" as some kind of vague analogy; but the if analogy is vague, why bother with it? But then PHP is full of silly ideas.

Ira Baxter
  • 93,541
  • 22
  • 172
  • 341
7

The table below is an easy reference for what these functions will return for different values. The blank spaces means the function returns bool(false). enter image description here

refer this link for more https://www.virendrachandak.com/techtalk/php-isset-vs-empty-vs-is_null/

DEVARAJ JOHNSON
  • 235
  • 3
  • 13
1

NULL is a special value which explicitly states that the variable has not been set to any value yet. Be careful with using the empty() function as you can't just determine that a variable is exactly NULL using it. For example the empty() function will return true if an int is set to 0. If you need to make sure a variable is exactly NULL use if($variable == NULL).

For more info on empty() see http://php.net/manual/en/function.empty.php

Chris
  • 7,996
  • 11
  • 66
  • 98
1

There are some good answers here, which I won't repeat. In the case of validating forms, though, when a form is submitted, the value of each form input element is sent to the server in the $_POST variable. You can check for the existence of a particular input by using isset().

isset($_POST['username'])

If this returns true, then this request to the server was the result of posting a form containing an input element named "username". Now that we know that we have a value for that form element, we can see if it has a valid value. empty() will tell us whether the user actually entered any data in the field, or whether they left it empty.

empty($_POST['username'])

If that returns true then the form submitted to the server had a field named "username" but the user didn't enter anything into before submitting the form.

Andrew Cooper
  • 32,176
  • 5
  • 81
  • 116
  • Ok, so rather than validate if the form was submitted using isset why not just use empty to validate if the form was submitted but also if the user entered any data? – PeanutsMonkey Apr 11 '11 at 00:55
  • You can. `empty()` will return true in any case that `isset()` does. Which one you use really depends on the semantics you're testing for at the time. – Andrew Cooper Apr 11 '11 at 01:13
  • Okay. I don't follow the semantics it depends on. How will I know or how do I know what the semantics are? – PeanutsMonkey Apr 11 '11 at 01:24
  • "Semantics" refers to the meaning you're trying to get out of the test. If all you care about was that a form was submitted with valid (non-empty) data, then use `empty()`. If you want to check that the form was actually posted, or that the form contained a particular field then you need to check whether a POST variable actually exists, so you use `isset()`. – Andrew Cooper Apr 11 '11 at 01:32
  • I think I follow what you mean. I should the appropriate functions based on the condition I would like to return true for. Is that correct? For example I know that $_POST; would return the value false if I use the empty() function, I would be better off using isset. – PeanutsMonkey Apr 11 '11 at 01:45
  • Yes. That's more-or-less it. – Andrew Cooper Apr 11 '11 at 01:47
0

This concept can be better understood from mathematics. Have you ever tried dividing a number (not zero) by 0 using a calculator e.g 7/0? You will get a result that looks like something this: undefined, not a number, null etc. This means that the operation is impossible, for some reasons (let's leave those reasons to be discussed another day).

Now, perform this: 0/7. You will get the output, 0. This means that the operation is possible and can be executed, but you the answer is just 0 because nothing is left after the division. There is a valid output and that output is zero.

In the first example, not only was the output invalid, the operation was not possible to execute. This is akin to null. The second example is akin to empty.

0

Been awhile since i used PHP but if other languages are anything to go by empty will indicate an existing object/map/array that has no contents while null would indicate a variable that has no meaning/definition at all (uninitialised).

In database SQL, NULL means "no value".

SpliFF
  • 38,186
  • 16
  • 91
  • 120
0

The empty() is a nice fast way to see if the variable holds any useful info... that is for strings empty() returns true for a string of "" as well as a null string.

So you can write something like this:

if (! empty($name)) echo $name;

More info see here: PHP: empty()

pmobley
  • 113
  • 1
  • 6
0

isset() returns true if both these conditions are met:

  1. The variable has been defined and has not yet been unset.
  2. The variable has a non-null value in it.

A variable is automatically defined when it gets set to something (including null). This has a direct implication in arrays.

$a=array();
$a['randomKey']=true;
$a['nullKey']=null;
var_dump(isset($a['randomKey'])); // true
var_dump(isset($a['nullKey'])); // true, the key has been set, set to null!
var_dump(isset($a['unsetKey'])); // false !
unset($a['randomKey']);
var_dump(isset($a['randomKey'])); // false ! it's been unset!

From above, you can check if various $_POST fields have been set. For example, a page that has been posted to, stands to reason, has the submit button name in the $_POST field.

empty() on the other hand, tests if the variable holds a non zero value. This means that values that (int) cast to 0, return false too. You can use this to see if a specific $_POST field has data in it.

Khez
  • 10,172
  • 2
  • 31
  • 51