-1

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.

Cain Nuke
  • 2,843
  • 5
  • 42
  • 65
  • Use [`isset()`](http://php.net/manual/en/function.array-key-exists.php) or [`isset()`](http://php.net/manual/en/function.array-key-exists.php) to verify if the parameter `s` is present in the query string before using `$_GET['s']`. – axiac Nov 08 '18 at 05:39
  • @FrozenFire `in_array()` searches for values in array, it doesn't care about its keys. Read about [PHP arrays](http://php.net/manual/en/language.types.array.php). – axiac Nov 08 '18 at 05:40
  • Sorry it was `array_key_exists()` http://php.net/manual/en/function.array-key-exists.php. – FrozenFire Nov 08 '18 at 05:41

1 Answers1

1

You can check your $_GET['s']

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

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

Ashish Yadav
  • 1,901
  • 2
  • 17
  • 23