Page 1 of 1

PHP Notice: Undefined index in the error log.

Posted: Sat May 03, 2008 9:46 pm
by srirams
Hi,

I have a calendar page which actually loads fine.
The calendar code starts like this.

Code: Select all

<?php 
require_once("include/inc_calendar.class.php"); 
$oCalendar = new inc_calendar(); 
$month = $_REQUEST['month']; 
$year = $_REQUEST['year']; 
$oCalendar->dateNow($month,$year);
?>
Though the calendar loads without any errors/warnings each time I navigate to this page but the errorlog logs the below notice.
I believe that the notice is indicating that the index value month is not set.

Code: Select all

[Sat May 03 14:29:50 2008] [error] [client 127.0.0.1] PHP Notice:  Undefined index:  year in C:\\Program Files\\Apache Group\\Apache2\\htdocs\\mysite\\view_calendar.php on line 5, referer: http://localhost:8080/mysite/menu.php 
 
[Sat May 03 14:29:50 2008] [error] [client 127.0.0.1] PHP Notice:  Undefined index:  month in C:\\Program Files\\Apache Group\\Apache2\\htdocs\\mysite\\view_calendar.php on line 5, referer: http://localhost:8080/mysite/menu.php

Re: PHP Notice: Undefined index in the error log.

Posted: Sun May 04, 2008 12:09 am
by yacahuma
do a print_r on the request. at some point you are not sending the index year and month

Re: PHP Notice: Undefined index in the error log.

Posted: Sun May 04, 2008 12:18 am
by Kastor
Yes, it is most likely that the index value month is not set and $oCalendar->dateNow($month,$year) takes $month as null or 0

Code: Select all

<?php
  echo “<pre>”
  print_r($_REQUEST);
  // or
  if (!isset($_REQUEST[‘month’])) die(‘Month is not set’);
?>

Re: PHP Notice: Undefined index in the error log.

Posted: Sun May 04, 2008 3:05 am
by lafever
That's because you're using undefined variables in the code. The $_REQUEST is not set so the values of $month and $year are null. You could do something simply like so.

Code: Select all

 
$month = isset($_REQUEST['month']) ? (int)$_REQUEST['month'] : date('m');
$year   = isset($_REQUEST['year']) ? (int)$_REQUEST['year'] : date('Y');
 

Re: PHP Notice: Undefined index in the error log.

Posted: Mon May 05, 2008 12:14 am
by srirams
Hi,

Thanks all for the responses.
Lafever's below code helped. Good sharp thinking mate. Much appreciated.