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.

 

Want to learn PHP 7?

Hacking with PHP has been fully updated for PHP 7, and is now available as a downloadable PDF. Get over 1200 pages of hands-on PHP learning today!

If this was helpful, please take a moment to tell others about Hacking with PHP by tweeting about it!

Next chapter: Controlling script execution >>

Previous chapter: How to read function prototypes

Jump to:

 

Home: Table of Contents

Copyright ©2015 Paul Hudson. Follow me: @twostraws.