if ((!$_GET['month']) && (!$_GET['year'])) {
$month = date ("n");
$year = date ("Y");
} else {
$month = $_GET['month'];
$year = $_GET['year'];
}
it shows Notice: Undefined index: month in.... .
I know that if I use error_reporting(null); above the code, the notice will not appear, but is there a way to fix this error?
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()orempty()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,emptymakes more sense; but you could also use!isset(), then0and empty strings will be allowed, too.However, it might make more sense to check those two variables separately: