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 = "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"; } ?> |
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 = "GeeksforGeeks"; // 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"; } ?> |
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 = "GeeksforGeeks"; // 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"; } ?> |
Value of variable is set
Recommended Posts:
- PHP | Check if a variable is a function
- How to check whether the enter key is pressed in a textbox or not using JavaScript / jQuery ?
- How to check whether a value is numeric or not using jQuery ?
- How to check a variable is of function type using JavaScript ?
- How to check whether an array is empty using PHP?
- Check whether a given Number is Power-Isolated or not
- How to check whether a script is running under Node.js or not ?
- How to check whether an image is loaded or not ?
- JavaScript | Check the existence of variable
- JavaScript | Check if a variable is a string
- How to check if a variable is an array in JavaScript?
- How to call PHP function from string stored in a Variable
- How to declare a global variable in PHP?
- How to get a variable name as a string in PHP?
- How to get cookies from curl into a variable in PHP ?
- PHP | Unset Session Variable
- Variable-length argument list in PHP
- How to change the value of a global variable inside of a function using JavaScript ?
- How to add a property to a JavaScript object using a variable as the name?
- JavaScript | Set object key by variable
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.

