Wrox Programmer Forums
| Search | Today's Posts | Mark Forums Read
Beginning PHP Beginning-level PHP discussions. More advanced coders should post to the Pro PHP forum.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the Beginning PHP section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
  #1 (permalink)  
Old August 17th, 2004, 01:16 PM
Registered User
 
Join Date: Aug 2004
Location: , , .
Posts: 2
Thanks: 0
Thanked 0 Times in 0 Posts
Send a message via MSN to ashinotani
Default Undefined index

Hi there:

I'm getting this error: Notice: Undefined index: bookcode in C:\www\html\hdr\index.php on line 78

Can someone tell me what I can do to fix it?

Source Code:

switch ($act) {

case $act == 'select':
// for now: store the bookcode selection in the 'state'
$cSelection = $_Session->getVar('cSelection');

if (empty($cSelection)) $cSelection = array();
// Line 78
$cSelection['bookcode'] = $gpv['bookcode'];
$_Session->setVar('cSelection',$cSelection);

break;

default:
$nextscreen = 'select';

} // end switch $act

Thanks,
-Ashi

  #2 (permalink)  
Old August 17th, 2004, 02:42 PM
Registered User
 
Join Date: Aug 2004
Location: , , .
Posts: 2
Thanks: 0
Thanked 0 Times in 0 Posts
Send a message via MSN to ashinotani
Default

Hey I found answer to my question:

The easiest way to get rid of those messages would probably be to add the following line in the beginning of the script:
error_reporting(E_ALL ^ E_NOTICE);

  #3 (permalink)  
Old August 17th, 2004, 03:31 PM
richard.york's Avatar
Wrox Author
Points: 5,506, Level: 31
Points: 5,506, Level: 31 Points: 5,506, Level: 31 Points: 5,506, Level: 31
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Jun 2003
Location: Camby, IN, USA.
Posts: 1,706
Thanks: 0
Thanked 6 Times in 6 Posts
Default

The easiest way to get rid of the errors is to correct them in the script. In production its always best to write scripts with error_reporting set to E_ALL.

Here's what's going on.

PHP is a very loose, flexible language. It doesn't require variables to be initialized, that is to be created before they are used or referenced in a script.

It isn't a good idea to suppress these errors, because they are, after all, errors. There's sound reasoning behind the PHP group deciding that these types of errors need to be reported.

Take for instance this example:

$variable = 1;

if ($varaible == 1)
{
    // do stuff
}

Its a common mistake to mispell a variable, if NOTICE errors are suppressed you'd have no idea what happened. With NOTICE errors activated, PHP complains that '$varaible' doesn't exist, alerting you to the mispelled variable. Put this in the context of a 2000 line script and you begin to see why it is useful.

So, NOTICE errors like this alert you that you've used a variable that doesn't exist.

There are ways of supressing them in a script without supressing the error reporting, and that is to use the language constructs isset() and empty().

isset() is like a function, but it isn't a function, it's a language construct. It preforms a special task, like echo or print. isset determines whether a variable exists, if a variable exists it returns TRUE, if the variable doesn't exist it returns FALSE. empty, which is closely related determines whether a variable has a value, if a variable exists and has a value other than 0, NULL, FALSE, "", it returns FALSE, if the variable doesn't exist, or doesn't have a value, e.g. is NULL, empty, FALSE, 0, "" (the values indicating absense of value) it returns TRUE. Use of these constructs don't trigger NOTICE errors.

To see this in action...

if (isset($variable) && !empty($variable))
{
    // $variable exists and isn't empty.. Do stuff.
}
else
{
    // Create some error reporting to alert you to the
    // the fact that $variable doesn't exist or is empty
    // or do other stuff when the $variable doesn't exist
    // or is empty.
}

This is part of the process of identifying places in your script where errors can potentially arise, and where you would take the opportunity to eliminate the possibility of those errors by implementing validation of your variables to ensure that they contain the values that you expect, whereas you may write the code to alert you or the user when things like this arise.

For reference:
http://www.php.net/isset
http://www.php.net/empty

By ensuring that the variables that you create contain the values that you expect you also , naturally, create more secure code. You can read up on this here:

http://www.php.net/manual/en/security.errors.php

HTH!

Regards,
Rich

--
[http://www.smilingsouls.net]
[http://pear.php.net/Mail_IMAP] A PHP/C-Client/PEAR solution for webmail


Similar Threads
Thread Thread Starter Forum Replies Last Post
Notice: Undefined index: username in latinquarter PHP Databases 1 April 18th, 2008 11:37 AM
Chapter 13 problem(undefined index) linux-penguin BOOK: Beginning PHP5, Apache, and MySQL Web Development ISBN: 978-0-7645-7966-0 2 February 15th, 2008 06:10 AM
undefined index and variable p3allen2000 Pro PHP 1 November 1st, 2007 04:49 PM
Undefined index junemo Pro PHP 8 October 31st, 2007 05:45 PM
Undefined Index Error rit01 Beginning PHP 0 March 1st, 2006 10:03 AM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.