Documentation

Is_set

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

The function checks if a variable (or an array key) exists. Returns true, if the variable var is defined, or a key is found among the keys of the var array. Otherwise, returns false.

Note. When checking if an array element exists, note that this function returns "true", even if the found array element has the "null" value, unlike the standard ''isset'' function (see the example below).

Function parameters

ParameterDescription
var An arbitrary type of variable can be passed in this parameter. If the var parameter is an array, the key must contain the key, which existence will be checked. If the var is not an array, the function will simply check if this variable is initialized or not (in this case, the function operates identically to the isset PHP function).
key If array is specified in var, this parameter must contain the key to check its availability in var array.

Example of use

<?

/********************************************
            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 initialized.';
else echo 'The key "a" of array $ar is not initialized.';
// output: "The key "a" of array $ar is initialized."

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."

// НО!
if (isset($ar["b"])) echo 'The key "b" of array $ar is initialized.';
else echo 'The key "b" of array $ar is not initialized.';
// (!) на экране будет: "output: "The key "b" of array $ar is not initialized".


if (is_set($ar, "с")) 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 initialized.';
else echo 'Variable $init_var is not initialized.';
// на экране будет: "Variable $init_var is initialized."

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


© «Bitrix24», 2001-2022
Up