JasonDavis JasonDavis - 2 months ago 8
PHP Question

How to get rid of hundreds of PHP undefined index notices?

I just enabled error reporting and wow what a shocker I have probably thousands if not hundreds of notices like this

Notice: Undefined index: action in C:\webserver\htdocs\header.inc.php on line 18


I understand that they are because I am calling a variable withoutsetting it or whatever but is there an easier way to set for example if a page has 50 variables that it is reporting this, is there an easier way to code that page properly to fix them all?

And I don't mean to just hide them I think it would be best to fix them

here is an example of that line I posted

if ($_GET['p'] == "account.edit.topfriends" || $_GET['action'] == "newmember" || $_GET['p'] == "account.profile.name") {
//some more code here
}

Answer

I usually like to use ternary statements at the top of my scripts to initialise values.


$_GET['p'] = (isset($_GET['p']) ? $_GET['p'] : 'default');

Sure you could probably use a more generic approach but that method can prove troublesome as different variables can have different default values.

Comments