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
}
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.
As rezzif mentioned what you need to do is check with an isset() call. If you're using arrays a lot and don't want to go back and add a bunch of isset() calls you can always us a function. Something like:
function get_index($array, $index) {
return isset($array[$index]) ? $array[$index] : null;
}
Then you could change your if-statement to something like:
if (get_index($_GET, 'p') == "account.edit.topfriends" || get_index($_GET, 'action') == "newmember" || get_index($_GET, 'p') == "account.profile.name") {
//some more code here
}
If all the checks being done are against $_GET
you could always nix the first parameter of the function and hardcode $_GET in it, my example assumes you're doing this against several different arrays.
This solution isn't necessarily the most elegant, but it should get the job done.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With