Search code examples
phpvariablesisset

How to use isset function in php properly?


What is the difference and which is better?

This one:

 if(isset($_POST['name'])){

 }

or this one:

 $name = $_POST['name'];
 if(isset($name)){

 }

I wanna know if there are differences between the two codes, and which is more efficient to use. Thank you in advance!


Solution

  • Hi The first one is right

    if(isset($_POST['name'])){
    
     }
    

    This will check $_POST['name'] is set or not.

    but

    $name = $_POST['name'];
     if(isset($name)){
    
     } 
    

    this will check $name is set or not. and it will go inside if because whether $_POST['name'] has value or not you have declared $name. So this will give wrong result