PHP isset() function

The isset() function is a built-in function of PHP, which is used to determine that a variable is set or not. If a variable is considered set, means the variable is declared and has a different value from the NULL. In short, it checks that the variable is declared and not null.

This function returns true if the variable is not null, otherwise it returns false. Note that the null character ("\0") is considered different from the PHP NULL constant.

Note: An important point to be noticed that if a variable is unset using the unset() function, it will not be considered to be set for so long.

Syntax

Mixed denotes that the parameter may be multiple types.

Parameters

This function accepts one or more parameters, but it must contain atleast one parameter.

variable (required) - It is necessary to pass this parameter to this function because it holds that variable/element, which is to be checked.

… - More variables to be checked. These are optional to pass in this function.

Return Values

The isset() function returns a boolean value: It may be either true or false.

If the variable exists and does not contain a NULL value, it returns TRUE otherwise, it returns FALSE.

Changes

From PHP 5.4.0, non-numeric offsets of strings return FALSE.

Examples

Below some list of examples are given through which you can understand isset() much better-

Example 1

Output

The variable test is set, so it will print.
bool(true)

Example 2: Difference between null character and NULL constant

The null character ("\0") is different from the PHP NULL constant. With the help of below example, you can practically see the difference.

Output

Variable 'x' is set. bool(true)
bool(false)
Variable 'z' is set. bool(true)

Example 3: Use of unset()

In this example, we will use the unset() function to unset the variable. Look at the below example:

Output

It will print because variables are set.
bool(true) bool(true)

Variables after unset:
bool(false) bool(false)

Example 4: Difference between isset and !empty

The isset() and !empty() functions are operated same and both return same results. The only difference between them is that if the variable is not present, the !empty() function does not generate any warning.

Output

0 is checked by isset() function
1 is checked by !empty() function

Example 5: Checking multiple variables

Output

Variables are declared and also not null.

Example 5: isset() to check session variable

Output

Session is available, Welcome to javatpoint





Latest Courses