Friday, October 10, 2025
HomeLanguagesHow to check whether a variable is set or not using PHP...

How to check whether a variable is set or not using PHP ?

We have given a variable and the task is to check whether a variable var is set or not in PHP. In order to do this task, we have the following methods in PHP:

Approach 1: Using isset() Method: The isset() method returns True if the variable is declared and its value is not equal to NULL.
 

Syntax:

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

Example :

PHP




<?php
// PHP program to check whether 
// a variable is set or not 
    
$str = "neveropen"; 
    
// 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"; 
} 
?> 


Output

Value of variable is set 

Approach 2: Using !empty() Method: The empty() method returns True if the variable is declared and its value is equal to empty and not a set.

Syntax:

bool empty( $var )

Example :

PHP




<?php
// PHP program to check whether 
// a variable is set or not 
    
$str = "neveropen"; 
    
// Check value of variable is set or not 
if(!empty($str)) { 
    echo "Value of variable is set"; 
} 
else { 
    echo "Value of variable is not set"; 
} 
?> 


Output

Value of variable is set 

Approach 3: Using !is_null() Method: The is_null() method returns True if the variable is declared and its value is equal to Null and not a set.

Syntax:

bool empty( $var )

Example :

PHP




<?php
// PHP program to check whether 
// a variable is set or not 
    
$str = "neveropen"; 
    
// Check value of variable is set or not 
if(!is_null($str)) { 
    echo "Value of variable is set"; 
} 
else { 
    echo "Value of variable is not set"; 
} 
?>


Output

Value of variable is set 
RELATED ARTICLES

Most Popular

Dominic
32350 POSTS0 COMMENTS
Milvus
87 POSTS0 COMMENTS
Nango Kala
6718 POSTS0 COMMENTS
Nicole Veronica
11880 POSTS0 COMMENTS
Nokonwaba Nkukhwana
11941 POSTS0 COMMENTS
Shaida Kate Naidoo
6838 POSTS0 COMMENTS
Ted Musemwa
7099 POSTS0 COMMENTS
Thapelo Manthata
6794 POSTS0 COMMENTS
Umr Jansen
6794 POSTS0 COMMENTS