Undefined index

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
spacebiscuit
Forum Contributor
Posts: 390
Joined: Mon Mar 07, 2005 3:20 pm

Undefined index

Post by spacebiscuit »

Hi guys,

Can anyone help with this error which is driving me crazy. My pache server is reporting the following warning:

PHP Notice: Undefined index: pageno in c:\\program files\Apache2\\htdocs\\index_video.php on line 27

The code in question is:

Code: Select all

$limits=get_limits('video',$_GET['pageno']);
My script works as expected but I can't stop the warning from showing up in my srver logs.

Any help would very much be appreciated.

Thanks in advance,

Rob.
User avatar
John Cartwright
Site Admin
Posts: 11470
Joined: Tue Dec 23, 2003 2:10 am
Location: Toronto
Contact:

Re: Undefined index

Post by John Cartwright »

$_GET['pageno'] will only exist when you have ?pageno= in the url. More than likely, your initial page does not contain this value. It is better practice to check the existance of user input, and/or give a default value.


i.e.,

Code: Select all

 
$pageno = 1;
if (isset($_GET['pageno'])) {
   $pageno = (int)$_GET['pageno'];
} 
 
// or
 
$pageno = isset($_GET['pageno']) ? (int)$_GET['pageno'] : 1;
spacebiscuit
Forum Contributor
Posts: 390
Joined: Mon Mar 07, 2005 3:20 pm

Re: Undefined index

Post by spacebiscuit »

Thanks for the prompt reply and I now understand why Apache is outputting the warning.

You're correct in that when my script is initially called the 'pagno' variable is not set. I'm not passing variables via URL as I'm using sessions variables instead. Therefore I simply have to ensure that the 'pageno' variable is initialised as follows:

Code: Select all

if(!isset($_GET['pageno'])){
     $_GET['pageno']=1;
                           }
Then the warning ceases!

Many thanks,

Rob.
Post Reply