Isset

  • June, 2021
  • 0 Views
  • 5
  • 2

isset() is an PHP inbuilt function. As per this function name "is" + "set" mean 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>

1. isset vs unset : Difference between isset() and unset() in php

isset as defined above , used to check whether variable defined/exist or not.

On the other side unset() is php inbuilt function which is used to destroy/unset variable.
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";
}
    
Ouput Before Unset the variable ==> var1 Exist

unset($var1);


if (isset($var1)) {
    echo "var1 Exist";
} else {
      echo  "var1 Not Exist";
}

Ouput After Unset the variable==> var1 Not Exist

No comments posted yet. Be the first to post comments. Thanks

Write Your Comments

2. 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 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.

Write Your Comments

3. 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 above as below

$isValid = false; 
if(isset($isValid) { } 

Evaluate the $isValid value (For example we assume false value here) and assign the result to $isValid. Further we can check for the variable $isValid passed in isset().

Write Your Comments

4. 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, else it will return false if any of 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 exist and defined
    
$a = 'a';
$a = 'b';
if (isset($a, $b) ) {
    echo 'All Params Set';
} else {
    echo 'Some or None set';
}

// Output : All Params Set

Write Your Comments

5. isset vs empty : Difference between isset() and empty() function in php

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

empty() is a php inbuilt funtion. It used to determine whether variable is empty or not.

Description:  
  • Return type : Boolean
  • Determine variable is empty
  • It return 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)

Write Your Comments