Core Function IsSet

From Sputnik Wiki
Jump to: navigation, search
IsSet( $variable )

Contents

Description

Determine if a variable is set and is not NULL

If a variable has been unset with Unset(), it will no longer be set.

IsSet() will return FALSE if testing a variable that has been set to NULL.

Also note that a NULL byte ("\0") is not equivalent to the Sputnik SV NULL type.

If multiple parameters are supplied then IsSet() will return TRUE only if all of the parameters are set.

Evaluation goes from left to right and stops as soon as an unset variable is encountered.

Parameters

$variable

The variable to be checked.

You may add as many variables to be checked as you wish separate them with ,

Return Value

Success - Returns true.

Failure - Returns false.

Remarks

A classes __IsSet() function will be called when IsSet() is used on a class if there is no __IsSet() function IsSet() will return true for the class otherwise it will use the return value of __IsSet() and check if that's true.

Example

A few examples

$var = '';
 
// This will evaluate to TRUE so the text will be printed.
if (isset($var)) 
{
    echo "This var is set so I will print.\n";
}
 
// In the next examples we'll use var_dump to output
// the return value of isset().
 
$a = "test";
$b = "anothertest";
 
say isset($a);      // TRUE
say isset($a, $b); // TRUE
 
unset ($a);
 
say isset($a);     // FALSE
say isset($a, $b); // FALSE
 
$foo = NULL;
say isset($foo);   // FALSE

This also work for elements in arrays:

$a = array ('test' => 1, 'hello' => NULL, 'pie' => array('a' => 'apple'));
 
say isset($a['test']);            // TRUE
say isset($a['foo']);             // FALSE
say isset($a['hello']);           // FALSE
 
// The key 'hello' equals NULL so is considered unset
// If you want to check for NULL key values then try: 
say IsKeySet($a, 'hello'); // TRUE
 
// Checking deeper array values
say isset($a['pie']['a']);        // TRUE
say isset($a['pie']['b']);        // FALSE
say isset($a['cake']['a']['b']);  // FALSE

Yet another example

IsSet($a) # Check if $a is set
IsSet($a['Test']) # Check if $a (as array) index 'Test' is set
IsSet($a['Test']['Cat']) # Check if $a (as array) index 'Test' (as array) index 'Cat' is set
Personal tools
Namespaces
Variants
Actions
Navigation
Toolbox