How to get rid of undefined index in this situation

Hi,

I have this code:

if($_GET['s']=="page1" || $_GET['s']=="page2" || $_GET['s']=="page3") {
 dosomething();
}

and I get this error: : Undefined index: s in

Which I can dismiss only by adding this line:

$_GET['s']="";

but then this wont execute the code correctly since $_GET['s'] is not supposed to have any initial value. How do I fix this other than disabling the notices and errors?

Thank you.

1 answer

  • answered 2018-11-08 05:36 ashiish.me

    You can check your $_GET['s']

    if(isset($_GET['s'])) {
      // your code goes here...
    }
    

    isset() is used to check if the index exists.