Open In App

PHP isset() Function

Last Updated : 24 Sep, 2024
Summarize
Comments
Improve
Suggest changes
Like Article
Like
Share
Report
News Follow

The isset() function in PHP checks whether a variable is declared and not NULL. It returns true if the variable exists and has a non-NULL value, and false otherwise, without modifying the variable.

Syntax

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

Parameters:

This function accept one or more parameter as mentioned above and described below:

  • $var: It contains the variable which need to check.
  • $…: It contains the list of other variables.

Return Value: It returns TRUE if var exists and its value not equal to NULL and FALSE otherwise. Below examples illustrate the isset() function in PHP:

Example 1: In this example we checks if the $str variable is set using isset(). It also verifies if the first element of an empty array $arr is set, printing appropriate messages for both checks.

<?php
    $str ="GeeksforGeeks";    
    
    // Check value of variable is set or not
    if(isset($str)){
        echo "Value of variable is set";

    }else{
        echo "Value of variable is not set";

    }
    $arr = array();
    
    // Check value of variable is set or not
    if( !isset($arr[0]) ){
        echo "\nArray is Empty";

    }else{
        echo "\nArray is not empty";

    }

?>

Output
Value of variable is set
Array is Empty

Example 2: In this example we use isset() to check if the variable $num and specific elements in the $arr array are set. It outputs true for existing elements and false for non-existent ones.

<?php
    $num = 21;
    var_dump(isset($num));
    
    $arr = array(
    "a" => "Welcome",
    "b" => "to",
    "c" => "GeeksforGeeks"
    );
    var_dump(isset($arr["a"]));
    var_dump(isset($arr["b"]));
    var_dump(isset($arr["c"]));
    var_dump(isset($arr["Geeks"]));

?>

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

PHP isset() Function – FAQs

Does isset() work on array elements?

Yes, isset() can be used to check if a specific array element is set, and it returns true if the element exists and is not NULL.

What happens if isset() is called on an undefined variable?

If isset() is called on an undefined variable, it returns false and may generate a warning if error reporting is enabled.

Can isset() check multiple variables at once?

Yes, isset() can check multiple variables in a single call. It returns true only if all variables are set and not NULL. If any variable is not set, it returns false.

Does isset() work with object properties?

Yes, isset() can be used to check if a property of an object is set, returning true if the property exists and is not NULL.

Does isset() distinguish between false and NULL?

Yes, isset() returns true for false values because false is a valid value. It returns false only if the variable is NULL or not set.



Similar Reads

three90RightbarBannerImg