Isset
isset() is a PHP inbuilt function. As per this function name "is" + "set" means it is used to check whether a variable is set or not.
- Return type : Boolean
- Check if passed variable is set or not
- Can check more than one variable to be defined or not
Example 1
<code class="language-html" data-lang="php">
$var = "Demo";
if (isset($var)) {
echo "Var Exist";
} else {
echo "Var Not Exist";
}
Output: Var Exist
</code>
<code class="language-html" data-lang="html">
if (isset($var)) {
echo "Var Exist";
} else {
echo "Var Not Exist";
}
Output: Var Not Exist
</code>
Scroll for More Useful Information and Relevant FAQs
- Is it possible to pass multiple parameters to the isset() method?
- isset vs empty : Difference between isset() and empty() function in php
- isset vs unset : Difference between isset() and unset() in php
- How to check if form has been submitted or submit button clicked?
- What is the cause behind PHP Fatal error: Cannot use isset() on the result of an expression and how to resolve it?
- Feedback: Your input is valuable to us. Please provide feedback on this article.
Is it possible to pass multiple parameters to the isset() method?
Yes
Multiple params can be checked whether set or not using isset(). Isset() will return true if all passed params are set otherwise it will return false if any of the passed params does not exist (set).
Here is the example for better understanding
//Case 1: No param defined
if (isset($a, $b) ) {
echo 'All Params Set';
} else {
echo 'Some or None set';
}
// Output: Some or None set
//Case 2: one of param defined
$a = 'a';
if (isset($a, $b) ) {
echo 'All Params Set';
} else {
echo 'Some or None set';
}
// Output: Some or None set
//Case 3: All param exists and defined
$a = 'a';
$a = 'b';
if (isset($a, $b) ) {
echo 'All Params Set';
} else {
echo 'Some or None set';
}
// Output: All Params Set
isset vs empty : Difference between isset() and empty() function in php
empty() is a php inbuilt funtion. It is used to determine whether a variable is empty or not.
Description:
- Return type : Boolean
- Determine variable is empty
- It returns true for variable value 0, "" (Empty String), false, null
Example
<?php
$var1 = 0;
var_dump(empty($var1)); // Output: bool(true)
$var2 = "";
var_dump(empty($var2)); // Output: bool(true)
$var3 = false;
var_dump(empty($var3)); // Output: bool(true)
$var4 = null;
var_dump(empty($var4)); // Output: bool(true)
isset vs unset : Difference between isset() and unset() in php
On the other side unset() is PHP inbuilt function that is used to destroy/unset variables.
Description:
- Return type : Void (Does not return)
- Destroy passed variable
<?php
$var1 = "Demo1";
if (isset($var1)) {
echo "var1 Exist";
} else {
echo "var1 Not Exist";
}
Output Before Unset the variable ==> var1 Exist
unset($var1);
if (isset($var1)) {
echo "var1 Exist";
} else {
echo "var1 Not Exist";
}
Output After Unset the variable==> var1 Not Exist
How to check if form has been submitted or submit button clicked?
What is the cause behind PHP Fatal error: Cannot use isset() on the result of an expression and how to resolve it?
isset() function in PHP is used to test whether a passing variable is set or not.
It will raise the parse error "PHP Fatal error: Cannot use isset() on the result of an expression" if we try to pass an expression.
Wrong way of using isset
if(isset($isValid === false)) { }
Correct way:
We can solve the above as below
$isValid = false;
if(isset($isValid) { }
Evaluate the $isValid value (For example we assume a false value here) and assign the result to $isValid. Further, we can check for the variable $isValid passed in isset().