Reputation: 8608
I have a php notice stating undefined offset:1
on line 5
(in this example).
1 private function checkForActiveControllerAndAction($filename, $navigation_controller_and_action) {
2
3 $splitted_filename = explode("/", $filename);
4 $active_controller = $splitted_filename[0];
5 $active_action = $splitted_filename[1];
6 $splitted_filename = explode("/", $navigation_controller_and_action);
7 $navigation_controller = $splitted_filename[0];
8 $navigation_action = $splitted_filename[1];
9 if ($active_controller == $navigation_controller AND $active_action == $navigation_action) {
10 return true;
11 } else {
12 return false;
13 }
14 }
What's causing it and how can I prevent this?
Upvotes: 1
Views: 69
Reputation: 7586
When you explode the $filename
the string may not contain a /
character, which means the whole of $filename
will remain in the first element of the array (by default).
You should check the length of the array count
or check if the element exists isset
.
This is what causes the unknown offset error.
Readup on explode here: https://www.php.net/explode
To check if you can access the 2nd index:
if (isset($splitted_filename[1])) {
// Code
}
Upvotes: 3