isset()

Description

This function determines if a passed variable has been set, that is, it has a non-NULL value.

Syntax


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

Parameters

$var
One or more variables to test can be passed to the function.

Return Value

bool
The function return one of the following values:

  • true if all of the passed variables are set.
  • false if at least one of the variables is not set. The variables are evaluated from left to right in the order passed. When the first unset variable is found, evaluation stops and the function returns false.

Examples

1. Scalar Types

This example creates variables of int, float, and string types. It then displays (using var_dump()), the value of isset() when each variable is passed. The example goes on to unset one of the variables, assign another the null value, and assign the third a null string.

<?php
$intVal = 12;
$floatVal = 123.45;
$stringVal = "Hello, world!";
var_dump( isset( $intVal ) );
var_dump( isset( $floatVal ) );
var_dump( isset( $stringVal ) );

unset( $intVal );
var_dump( isset( $intVal ) );
$floatVal = null;
var_dump( isset( $floatVal ) );
$stringVal = "";
var_dump( isset( $stringVal ) );
?>

The results below show that all three variables are initially set after their creation. The variable becomes unset after passing it to the unset() function or explicitly setting it to null. However, the string variable is still set even after setting its value to the null string (which is not the same thing as setting it to null).

bool(true)
bool(true)
bool(true)
bool(false)
bool(false)
bool(true)

2. Passing Multiple Variables

In this example, three variables are created and passed in one call to isset() and the output is shown with var_dump(). Then the second variable is unset by passing it to unset() and the same isset() call is repeated.

<?php
$intVal = 12;
$floatVal = 123.45;
$stringVal = "Hello, world!";
var_dump( isset( $intVal, $floatVal, $stringVal ) );

unset( $floatVal );
var_dump( isset( $intVal, $floatVal, $stringVal ) );
?>

The output below shows that isset() returns true when all three variables are set, but returns false if any one of them is not set.

bool(true)
bool(false)

3. Passing Arrays

4. Passing String Offsets

See Also

References