User Name
Password
AppleNova Forums » Programmer's Nook »

PHP Notice: Undefined index - Elegant avoidance..


Register Members List Calendar Search FAQ Posting Guidelines
PHP Notice: Undefined index - Elegant avoidance..
Thread Tools
scratt
Veteran Member
 
Join Date: Jul 2004
Location: M-F: Thailand Weekends : F1 2010 - Various Tracks!
Send a message via Skype™ to scratt 
2008-10-08, 11:04

Any PHP gurus here?

I am trying to get my new server setup really tight.

A script I use generates a lot of PHP Notice: Undefined index errors for undefined variables in one of it's functions.

I know I can suppress these errors, and ignore them, but I don't like band aid solutions.

Is there an elegant solution to making sure that these variables don't give that error other than manually defining them all earlier in the script?

As you can probably tell I am a php hacker at best, not a real php programmer!
Cheers for any tips..

'Remember, measure life by the moments that take your breath away, not by how many breaths you take'
Extreme Sports Cafe | ESC's blog | scratt's blog | @thescratt
  quote
Kraetos
Lovable Bastard
 
Join Date: Dec 2005
Location: Boston-ish
 
2008-10-08, 12:06

Can we see the script?
  quote
Brad
Selfish Heathen
 
Join Date: May 2004
Location: Zone of Pain
 
2008-10-08, 12:56

Any time you see notices, you should fix them ASAP. PHP notices would in most other languages cause fatal errors and even in PHP they are indicative of potential bugs and problematic design.

If you want to conditionalize a piece of code that accesses the array and you're not sure if the key is set, as in the case of this particular error, try:
[php]<?php

$foo = array('bar'=>'123456');

if ( array_key_exists('baz', $foo) ) {
print $foo['baz'];
} else {
print "hello world";
}

?>[/php]

In this case, you should get "hello world" without any notices being triggered.

To completely suppress notices, if you're interested, you must change the "error_reporting" setting in your php.ini file. There should be lots of comments above that setting, but in case there are not, here are mine:

Code:
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; ; Error handling and logging ; ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; ; error_reporting is a bit-field. Or each number up to get desired error ; reporting level ; E_ALL - All errors and warnings (doesn't include E_STRICT) ; E_ERROR - fatal run-time errors ; E_WARNING - run-time warnings (non-fatal errors) ; E_PARSE - compile-time parse errors ; E_NOTICE - run-time notices (these are warnings which often result ; from a bug in your code, but it's possible that it was ; intentional (e.g., using an uninitialized variable and ; relying on the fact it's automatically initialized to an ; empty string) ; E_STRICT - run-time notices, enable to have PHP suggest changes ; to your code which will ensure the best interoperability ; and forward compatibility of your code ; E_CORE_ERROR - fatal errors that occur during PHP's initial startup ; E_CORE_WARNING - warnings (non-fatal errors) that occur during PHP's ; initial startup ; E_COMPILE_ERROR - fatal compile-time errors ; E_COMPILE_WARNING - compile-time warnings (non-fatal errors) ; E_USER_ERROR - user-generated error message ; E_USER_WARNING - user-generated warning message ; E_USER_NOTICE - user-generated notice message
Basically, you want to turn off for E_NOTICE.

The quality of this board depends on the quality of the posts. The only way to guarantee thoughtful, informative discussion is to write thoughtful, informative posts. AppleNova is not a real-time chat forum. You have time to compose messages and edit them before and after posting.
  quote
scratt
Veteran Member
 
Join Date: Jul 2004
Location: M-F: Thailand Weekends : F1 2010 - Various Tracks!
Send a message via Skype™ to scratt 
2008-10-08, 13:24

Brad,

Thanks for that..

I am aware of the error suppressing but agree with you totally that it's better to have a setup that runs without errors.

In a nutshell, are you suggesting that I define a variable in the form which then is used to make the defining of the variables in the php script conditional?

Would it be better to just split that stuff out of the config.php and run it separately?
Sorry, I am not that super-duper at the ins and outs of php / html interraction, so that may be a dumb question..

To clarify all of this is stuck inside the config.php file for this bunch of files.. and I have to wonder if that's a good idea or not..

Kraetos, here's a short excerpt from the script..

[PHP]<?

$db_user = "xxxxxxx"; // db user
$db_pass = "xxxxxxxxx"; // db pass
$db_host = "xxxxxxxxxxx";
$db = "xxxxxxxxxxx"; // db

$adminmail = "xxxxxxxxxx"; // admin email

$year = date( "Y" );
$posttime = date( "H:i");
$postdate = date( "Y-m-d" );
$photostamp = date( "YmdHi" );

$q = $_GET['q'];
$id = $_REQUEST['id'];
$password = $_REQUEST['password'];
$category = $_REQUEST['category'];
$photo = $_REQUEST['photo'];

$name = $_POST['name'];
$email = $_POST['email'];
$address = $_POST['address'];

....
[/PHP]

Basically everything from $year down should be inside some kind of condition I think, as Brad suggests, again I think!

I am assuming that the config.php is called anytime a database access is made, which happens to include when a form is submitted to provide a new entry.

I know it's a crappy script, but it's perfect for what I need and I would rather help it out, than write the whole thing from scratch... PHP is still a bit of a black art to me apart from hacking around in it a bit!

'Remember, measure life by the moments that take your breath away, not by how many breaths you take'
Extreme Sports Cafe | ESC's blog | scratt's blog | @thescratt

Last edited by scratt : 2008-10-08 at 13:39.
  quote
Kraetos
Lovable Bastard
 
Join Date: Dec 2005
Location: Boston-ish
 
2008-10-08, 15:42

Yep, Brad is right, the database entries need to be conditionalized. A stricter programming language would crash the entire script. Java, for example, would throw a NullPointerException and exit. Putting all that in a conditional would fix it.

Logic, logic, logic. Logic is the beginning of wisdom, Valeris, not the end.
  quote
Posting Rules Navigation
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Post Reply

Forum Jump
Thread Tools

« Previous Thread | Next Thread »

All times are GMT -5. The time now is 12:42.


Powered by vBulletin®
Copyright ©2000 - 2024, Jelsoft Enterprises Ltd.
Copyright ©2004 - 2024, AppleNova