Hudzilla.org - the homepage of Paul Hudson
Contents > Functions Wish List | Report Bug | About Me ]

4.3     Working with variables: isset(), empty(), and unset()

This is NOT the latest copy of this book; click here for the latest version.

bool isset ( mixed var [, mixed var [, mixed ...]])

void unset ( mixed var [, mixed var [, mixed ...]])

bool empty ( mixed var)

There are three very basic functions that help you use variables, and these are isset(), empty(), and unset(). Isset() and empty() are almost opposites, but not quite - isset() will return true if a variable, passed in as its only parameter, has been set in the script already, whereas empty(), which also takes a variable as its sole parameter, will return true if the variable has a false value. This is not the same thing!

To illustrate the difference consider the variable $foo being set to false - isset() would return true because $foo is a variable that has a value, and empty() would also return true because $foo's value is false. To check for "variable not set", use the not operator !, as in if (!isset($foo).

Unset() removes an existing variable entirely, so that isset() will return false. That variable can be recreated later on in the script, however. Use unset() when you want to delete a variable.





<< 4.2 How to read function prototypes   4.4 Controlling script execution: exit(), eval(), and die() >>
Table of Contents
Want to see this stuff in print? PHP in a Nutshell takes the core topics covered here, adds in thousands of edits from the editorial team and myself, and combines them to make an unbeatable reference for PHP programmers at all levels.



My latest book has hundreds more tips on how to use PHP, Apache, and MySQL, plus Perl, Python, shell scripts, performance tuning, and more!



Top-right shadow
 
Bottom-left shadow Bottom shadow

Comments from other readers
deathGod - 06 Sep 2008

and i don't know why people are still complaining. There has only been one or two hard pages we have come accross.

And thanks to all the people doing examples here, they really do help a lot.

deathgod - 06 Sep 2008

hey no swearing but i agree with you.

Tip:The best way to learn php is to adopt an open-source project written in php and experiment with it. I'm currently playing with osCommerce and i have a vague notion of half the things here even before i read them. Like for example prototypes: i had no idea what he said and it got me all confused but after 30minutes of re-reading the section i figured out i already knew what it was just by messing around with osCommerce.

Sweet isn't it!

A PHP User - 06 Sep 2008

http://www.usphp.com/about.prototypes.html
go here to learn ALL about reading and understand prototypes.

A PHP User - 06 Sep 2008

why don't you shut up complaining, the book is free, it's here without it It would have taken months ifyou don't have the mind for a programmer get the fuck out.

A PHP User - 06 Sep 2008

uhm... this book was good for about 3 sections. Now it sucks.


I give up.

Dominic Son - 06 Sep 2008

Here's my example of this section:

Let's make a variable and call it $mo_money.
- isset ($mo_money) returns 'true' if there was ANY amount set for $mo_money.
- empty ($mo_money) returns 'true' only if the amount is set to 0 - and in programming 0 is false.

If you can honestly picture the 2nd paragraph as a beginner, your mind has grown : P

Finally unset($mo_money) seems to delete the variable in this function..

TxtEdMacs - 06 Sep 2008

On the empty() function I was surprised it returnes a true when the boolean variable was set as false. However, more troubling (in the documentation) a 0, particularly an integer is considered empty. I must admit my previous use of an empty() function was testing for unset or null values, which would trash my computations. However, a zero integer value could be perfectly a valid set value, e.g. a beginning to an increment or even a cash balance. Not sure I like the php empty() function.

A PHP User - 06 Sep 2008

should i be in a different chapter? all of a sudden, i am LOST!! i was doing great, "flipped the page", then BAM .. Lost.

what do all of these prototypes do? or should i just read on in anticipation?

ugh.

A PHP User - 06 Sep 2008

Empty is sort of a confusing name. isfalse(); would probably make more sense, but anyways back to the book.

What is up with isset being able to have more than 1 parameter?

ljubiccica - 06 Sep 2008

one ) missing:

use the not operator !, as in if (!isset($foo).

should be

use the not operator !, as in if (!isset($foo)).

I LOVE THIS BOOK!!!

Jason/netnerd85@hotmail.com - 06 Sep 2008

Please add examples and further explanation to this section, cheers.



Add comment
Please note that by posting a comment here you are committing it to the public domain. This is important so that others can make use of your code themselves, and also so that I can incorporate helpful notes directly into the main text. Comments are limited to 2000 characters in length.

If you are reporting an error in the content, please tell me directly.

Your name/email address:
Your comment:
 
Now, in order to verify that you're a real person, please answer this simple question: what is four plus zero?
The answer is:
(please write in
numbers, eg 19)


Top-right shadow
 
Bottom-left shadow Bottom shadow