PHP isset() Function | What is isset in PHP and Use with Example?
Summary: In this article, you will learn what is PHP isset() function and how to use them. Therefore, also you will learn Why to check both isset() and !empty() function in PHP?
What is PHP isset() function?
PHP has two Identical functions that are necessary to writing PHP applications, but whose aim and exact function is rarely well explained: isset and empty. The PHP isset() function is used to checks whether a variable is set, which implies that it has to be declared and isn’t NULL. If a variable is already unset with the unset() function, it’ll now not be set.
This function returns true if the variable exists and isn’t NULL, otherwise, it returns false.
Syntax:
Parameter Value:
Parameter Value | Description | Type |
---|---|---|
variable1 | It is a required parameter, Specifies the variable being checked | Mixed* |
variable2 | It is optional parameter. furthermore, another more variable to be checked. | Mixed* |
variable3 | It is optional parameter. furthermore, another more variable to be checked. | Mixed* |
Return Value:
TRUE if variable (variable1, variable2, variable3) exists and has a value not equal to NULL, FALSE otherwise.
PHP isset() Function Example
Program:1
1 2 3 4 5 6 7 8 9 10 11 12 13 |
<!--?php $x = 0; // True because $a is set if (isset($x)) { echo "Variable 'x' is set.<br>"; } $y = null; // False because $b is NULL if (isset($y)) { echo "Variable 'y' is set."; } ?--> |
Program:2
1 2 3 4 5 6 7 8 9 10 11 12 13 14 |
<?php $var = ''; // This will evaluate to TRUE so the text will be printed. if (isset($var)) { echo "This var is set so I will print."; } // In the next examples we'll use var_dump to output // the return value of isset(). $x = "welcome"; $y = "mostwelcome"; var_dump(isset($x)); var_dump(isset($x, $y)); unset ($x); var_dump(isset($x)); var_dump(isset($x, $y)); $foo = NULL; var_dump(isset($foo)); ?> |
boolean true
boolean true
boolean false
boolean false
boolean false
Q.1 Why to check both isset() and !empty() function in PHP ?
Reason to check both function:- As you know that isset() and !empty() both function are similar. they will return Identical results. however, the only difference is !empty() function will not generate a warning when the variable doesn’t exist. It’s enough to use either of the function. Here, both functions in a program cause time-lapse and unnecessary memory usage.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 |
<?php // PHP function to demonstrate isset() // and !empty() function // Inintialize a variable $number = '0'; // Check isset() function if( isset ( $number ) ) { print_r( $number . " is set with isset function"); } // Display new line echo "\n"; // Inintialize a variable $number = 1; // Check the !empty() function if( !empty ( $number ) ) { print_r($number . " is set with !empty function"); }?> |
1 is set with !empty function
Conclusion:
In this article, you have learned PHP isset() and to check both isset() and !empty() function in PHP ? I hope you will enjoy it!. if have any query related program then contact me at info@tutorialscan.com I will try to resolve the problem.
Stay healthy, stay safe! Happy coding! enjoy it!