If the array element does not exist, you get the notice since you are trying to access a non-existent element. You need to use isset()
or empty()
to check it (those are not functions but language constructs so it's not considered accessing those elements). Since you probably never have empty/zero years/months, empty
makes more sense; but you could also use !isset()
, then 0
and empty strings will be allowed, too.
if(empty($_GET['month']) || empty($_GET['year'])) {
$month = date('n');
$year = date('Y');
}
else {
$month = (int)$_GET['month'];
$year = (int)$_GET['year'];
}
However, it might make more sense to check those two variables separately:
$month = empty($_GET['month']) ? date('n') : $_GET['month'];
$year = empty($_GET['year']) ? date('Y') : $_GET['year'];