Working with variables

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.

 

Next chapter: Controlling script execution >>

Previous chapter: How to read function prototypes

Jump to:

 

Home: Table of Contents

Follow us on Identi.ca or Twitter

Username:   Password:
Create Account | About TuxRadar