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

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

NOTE: 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
Top-right shadow
 
Bottom-left shadow Bottom shadow