-2

I know how to do the isset thing, and I also know that the notice itself is no biggie, but it still bothers my ocd.

This is how my page handling the variables in the mentioned lines is set up:

//this part gets the page name from the url or sets the name to "Home"
if (isset($_GET['page'])):
    $pageCurrent = ucfirst($_GET['page']);
else:
    $pageCurrent = 'Home';
endif;

//this is a multiple array to handle the error pages
$errorPages = array(
    '404' => array(
        'errorTitle' => 'title',
        'error' => 'content'    
    ),
    '403' => array(
        'errorTitle' => 'title',
        'error' => 'content'        
    )
);

//this two lines are the bastards giving me a hard time
$errorTitle = $errorPages[$pageCurrent]['errorTitle'];
$error = $errorPages[$pageCurrent]['error'];

You can view the whole thing on GitHub

Thanks

andyosuna
  • 18
  • 5
  • Is that a valid php code for if else? – kimbarcelona Aug 13 '14 at 02:42
  • 2
    @kimbarcelona Yes. http://php.net/manual/en/control-structures.alternative-syntax.php – Tieson T. Aug 13 '14 at 02:42
  • Got it. Well, I'm not used to use that syntax for if else. – kimbarcelona Aug 13 '14 at 02:45
  • 5
    Is it just me or... if your pageCurrent is set to 'Home', and you do errorPages['Home'] , that doesn't exist in your errorPages array... so... – olive_tree Aug 13 '14 at 02:45
  • of course its supposed to show that, what if `$pageCurrent == 'Home'`, then its undefined, just add another checking isset on `$errorPages[$pageCurrent]` – Kevin Aug 13 '14 at 02:46
  • @olive_tree The exact point/question I was about to make/ask. – Tieson T. Aug 13 '14 at 02:46
  • @olive_tree the error shows on anything but the error pages, so you are correct, but doing the isset on the $errorPages didn't fix it, I had tried it already... – andyosuna Aug 13 '14 at 02:52
  • @andyosuna what olive_tree is saying, that you're trying to reference `$errorPages['Home']['errorTitle']` and you haven't set this index in your array. You're essentially trying to make reference to something that doesn't exist in your array – Bankzilla Aug 13 '14 at 02:57
  • I had to step away and have a smoke for a minute, I had been staring at this for too long. I did an if statement checking for $pageCurrent to be equal to the values of the error pages, a little too bulky for my liking but that fixed it. Thank you guys! Ghost olive_tree and Bankzilla – andyosuna Aug 13 '14 at 03:05

1 Answers1

1

In your case, the error message is occurring because you are referencing a key/value that doesn't exist in your $errorPages array.

When accessing values in an array, you must be sure that the key you are using exists. In your case you can use isset for this:

if(isset($errorPages[$pageCurrent])) {

}

Alternatively you can use array_key_exists (documentation)

isset will return false if the key exists but is set to null. array_key_exists will only return false if the key has never been set, or has been unset in the array.

Scopey
  • 6,269
  • 1
  • 22
  • 34