How to get rid of undefined index in this situation

2689 views php
4

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.

answered question

Use isset() or isset() to verify if the parameter s is present in the query string before using $_GET['s'].

@FrozenFire in_array() searches for values in array, it doesn't care about its keys. Read about PHP arrays.

1 Answer

1

You can check your $_GET['s']

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

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

posted this

Have an answer?

JD

Please login first before posting an answer.

Ads

Categories