-2
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?

Mat
  • 202,337
  • 40
  • 393
  • 406
user1345545
  • 63
  • 1
  • 2
  • 9
  • 2
    `isset` `array_key_exists` and `empty` are your friends. – Corbin Apr 28 '12 at 08:25
  • possible duplicate of [PHP: "Notice: Undefined variable" and "Notice: Undefined index"](http://stackoverflow.com/questions/4261133/php-notice-undefined-variable-and-notice-undefined-index) – Corbin Apr 28 '12 at 08:26

5 Answers5

2

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'];
ThiefMaster
  • 310,957
  • 84
  • 592
  • 636
  • Showing how to properly do the logic he wants might be good. `if (!empty(...) && !(empty(...)) { $month = ...; $year = ...; } else { $month = date('n'); $year = date('Y'); }` – Corbin Apr 28 '12 at 08:30
  • $month = empty(intval($_GET['month'])) ? date('n') : $_GET['month']; – askovpen Apr 28 '12 at 08:41
  • @askovpen intval always returns at least (int)0 so it would never be empty – Lawrence Cherone Apr 28 '12 at 08:45
  • @Corbin: True, but I highly doubt he wants that as it could still leave him with a notice in case only one of the values is missing. – ThiefMaster Apr 28 '12 at 08:48
  • The way I've written it, it would not be possible for that block of code to generate a notice for an undefined index. Either both indexes are defined, or neither are accessed. – Corbin Apr 28 '12 at 08:53
  • @askovpen intval() would throw an undefined index notice if the month key did not exist. – Corbin Apr 28 '12 at 08:54
  • @LawrenceCherone empty() considers 0 empty. (And "0" interestingly enough.) – Corbin Apr 28 '12 at 08:54
1

The way you currently have it is check both at once and if one fails change both, perhaps it would be better to preset the month & date and then change if params are passed. Plus it would be a good idea to check there numeric. Else a string may break your code further along

<?php 
$month = date ("n");
$year = date ("Y");
if (isset($_GET['month']) && is_numeric($_GET['month'])) {
    $month = $_GET['month'];
}
if (isset($_GET['year']) && is_numeric($_GET['year'])) {
    $year = $_GET['year'];
}

//Or better yet
$month = (isset($_GET['month']) && is_numeric($_GET['month']))?$_GET['month']:date("n");
$year = (isset($_GET['year']) && is_numeric($_GET['year']))?$_GET['year']:date("Y");
?>
Lawrence Cherone
  • 46,049
  • 7
  • 62
  • 106
0

You have to use empty() or isset() to check if a variable has been defined.

if ( empty($_GET['month']) || empty($_GET['year']) ) {
   $month = date ("n");
   $year = date ("Y");
} else {
   $month = $_GET['month'];
   $year = $_GET['year'];
}
Nadh
  • 6,987
  • 2
  • 21
  • 21
0
 $month = date('n');
 $year = date('Y');
 if (isset($_GET['month'])) {
   $month=$_GET['month'];
 }
 if (isset($_GET['year'])) {
   $year=$_GET['year'];
 }
gopi1410
  • 6,567
  • 9
  • 41
  • 75
0

Yes you can use if(isset($_GET['month']) && isset($_GET['year'])) in your if block

ThiefMaster
  • 310,957
  • 84
  • 592
  • 636
Tuhin Subhra Dey
  • 970
  • 6
  • 19