QuizCure
Find Available Solution Here!

Isset

Deepak Dec 20, 2024

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.

Description:
  • Return type : Boolean
  • Check if passed variable is set or not
  • Can check more than one variable to be defined or not
Here is the example for more understanding

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>
Example 2
<code class="language-html" data-lang="html">
if (isset($var)) { 
echo "Var Exist"; 
} else { 
echo "Var Not Exist"; 
} 
Output: Var Not Exist
</code>

Is it possible to pass multiple parameters to the isset() method?

Yes

php isset with multiple parameters

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

isset() as defined above, is used to check whether a variable is set or not.

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

isset as defined above, is used to check whether a variable is defined/exists or not.

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
Example 1
<?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?

PHP isset() function can be used to determine whether a variable is set (clicked/pressed) or not. Let's understand with the below example:

 
<?php

if (isset($_POST['Submit'])) {
    echo "Form Submitted<br> Received Email:". $_POST['email'] ;
} else {
     echo "Form Not Submitted";
}

?>

<form method="post" name"frm">

    Enter Email:<input type="text" name="email" value="">
<input type="submit" value="Save" name="Submit">

</form>

isset($_POST['Submit']) used to checked whether Submit button is clicked.
Output : Form Submitted
Received Email:

Note: It is recommended to filter and apply validation rules to validate input data before processing further to avoid any form submission Vulnerabilities.

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)) { } 
Here expression $isValid === false is passed in isset() which will raise a parse error.

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().

Was this post helpful?

Send Feedback

Connect With QuizCure


Follow Us and Stay tuned with upcoming blog posts and updates.

Contributed By

Deepak

Deepak

QC STAFF
51 Posts
  • PHP
  • JAVA
  • PYTHON
  • MYSQL
  • SEO

You May Like to Read

Scroll up