Wrox Programmer Forums

Need to download code?

View our list of code downloads.

Go Back   Wrox Programmer Forums > PHP/MySQL > Beginning PHP
Password Reminder
| FAQ | Members List | Calendar | 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 tens of thousands of software programmers and website developers including Wrox book authors and readers. As a guest, you can read any forum posting. By joining today you can post your own programming questions, respond to other developers’ questions, and eliminate the ads that are displayed to guests. Registration is fast, simple and absolutely free .
DRM-free e-books 300x50
Thread Tools Search this Thread Display Modes
  #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'];


$nextscreen = 'select';

} // end switch $act


Reply With Quote
  #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

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);

Reply With Quote
  #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

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.
    // 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:

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://pear.php.net/Mail_IMAP] A PHP/C-Client/PEAR solution for webmail
Reply With Quote

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are Off
Pingbacks are On
Refbacks are Off

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

All times are GMT -4. The time now is 03:47 PM.

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