Bitrix Site Manager

is_set

bool
is_set(
 mixed &var,
 string key = false
)

The function is_set checks whether a variable or array key exists. If a variable var is set or a key can be found in the array var, the function returns true. Otherwise, false is returned.

Note
When checking for array key existence, note that the function returns true even if the array key has a value of "null" unlike the standard function isset (see Example below).

Parameters

ParameterDescription
var A variable of arbitrary type can be passed in this parameter. If the var parameter is an array, the key needs to contain the name of the key whose existence is to be checked. If it is not an array, the function check whether a variable with the passed name is initialised or not (identical to the PHP function isset).
key If the var parameter is an array, this parameter should contain the name of the key in array defined by var.

Example



<?

/********************************************
            Example with array
********************************************/

$ar = array("a" => "", "b" => null);

if (is_set($ar, "a")) 
    echo 'The key "a" is found in array $ar.';
else 
    echo 'The key "a" is not found in array $ar.';
//
// output: "The key "a" is found in array $ar."
//

if (isset($ar["a"])) 
    echo 'The key "a" of array $ar is initialised.';
else 
    echo 'The key "a" of array $ar is not initialised.';
//
// output: "The key "a" of array $ar is initialised."
//

if (is_set($ar, "b"))
    echo 'The key "b" is found in array $ar.';
else 
    echo 'The key "b" is not found in array $ar.';
//
// output: "The key "b" is found in array $ar."
//

// BUT!
if (isset($ar["b"]))
    echo 'The key "b" of array $ar is initialised.';
else 
    echo 'The key "b" of array $ar is not initialised.';
// (!) output: "The key "b" of array $ar is not initialised."


if (is_set($ar, "c"))
    echo 'The key "c" is found in array $ar.';
else 
    echo 'The key "c" is not found in array $ar.';
//
// output: "The key "c" is not found in array $ar."


/********************************************
  Example with variable (not array)
********************************************/

$init_var = "A";
if (is_set($init_var))
    echo 'Variable $init_var is initialised.';
else
    echo 'Variable $init_var is not initialised.';
//
// output: "Variable $init_var is initialised."
//

if (is_set($not_init_var))
    echo 'Variable $not_init_var is initialised.';
else
    echo 'Variable $not_init_var is not initialised.';
//
// output: "Variable $not_init_var is not initialised."
//
?>