PHP: isset()

isset() is a function testing if a variable $var is set or not.

boolean isset (mixed $var[, mixed $var[,...]])

It returns TRUE is the variable is set, and FALSE if it isn’t.

Using isset() for testing a variable saves some times compared to try to use an nonexistent one. It also helps to avoid error messages from PHP:

/* The line below generates the warning message:
* Notice: Undefined variable: var in /path/to/script.php on line 1
*/
$toto = $var;

/* Of course it's possible to silence with @ the warnings from PHP but it's costly */
$toto = @$var;

/* Do this instead: */
if (isset ($var)) {
$toto = $var;
}

As seen in the function definition above, isset can take multiple arguments. But it is faster to have an if statement with one isset() call per variable rather than one isset() call with several variables:

if (isset($a) && isset($b)) { } /* Fast */

if (isset($a, $b)) { } /* Slow */