1

I am using this line of code to check if isset is not set.

for example :

if(!isset($_REQUEST['search']))
{ }
else if(!isset($_REQUEST['submit']))
{}

I just want to know if !isset is a valid code. If not, what is the proper method? Thanks

Amlan
  • 129
  • 1
  • 2
  • 14
  • please read http://stackoverflow.com/questions/2281633/javascript-isset-equivalent – Renjith V R Feb 22 '16 at 09:38
  • 1
    It's perfectly fine to use `!isset`. `else ifif` On the otherside isn't. – Daan Feb 22 '16 at 09:38
  • @Anant Why exactly is it a "much better approach" ?, If you just want to check if something isn't set, then why write the whole `if else` construct and not just use `!isset`? – Daan Feb 22 '16 at 09:40
  • @Daan forgive my typo on "ifif" – Amlan Feb 22 '16 at 09:53
  • @Anant You're dodging the question. Why write the whole `if else` construct if you just want to check if something isn't set? "! is not a better thing" is just wrong. – Daan Feb 22 '16 at 09:55
  • @Anant what if i wanted to write like this if(!isset($_REQUEST['search'])) { } else if(!isset($_REQUEST['sumbit'])) { } – Amlan Feb 22 '16 at 10:03
  • @Anant all i wanted to know if !isset() valid from SO users – Amlan Feb 22 '16 at 10:04
  • @Anant thanks a lot Anant – Amlan Feb 22 '16 at 10:22

3 Answers3

1

Checking a variable is set

To make it work, you pass a variable in as the only parameter to isset(), and it will return true or false depending on whether the variable is set. For example:

<?php
    $foo = 1;
    if (isset($foo)) {
        echo "Foo is set\n";
    } else {
        echo "Foo is not set\n";
    }
    if (isset($bar)) {
        echo "Bar is set\n";
    } else {
        echo "Bar is not set\n";
    }
?>

That will output "Foo is set" and "Bar is not set". Usually if you try to access a variable that isn't set, like $bar above, PHP will issue a warning that you are trying to use an unknown variable. This does not happen with isset(), which makes it a safe function to use.

Renjith V R
  • 2,981
  • 2
  • 22
  • 32
0

It'd fine to use !isset, no issue, but else ifif is completly wrong in your code

Approach will be:-

if(isset($_REQUEST['search'])){  // check that variable is set and have some value or not
    // variable is set and have value
}else {
   // variable is either not set or not have  value
}
Alive to die - Anant
  • 70,531
  • 10
  • 51
  • 98
0

Here You are sending userName from post method.

 <?php
    if(isset($_POST['userName']))
    {
    //your code 
    header("location: dashoard.php");
    }
    else{
    // your code
    header("location: index.php");
    }
    ?>
sudhir
  • 11
  • 2