Undefined Variable, 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
Eric123
Forum Newbie
Posts: 5
Joined: Fri Dec 19, 2003 12:58 am

Undefined Variable, Undefined Index

Post by Eric123 »

I am novice, and wish to know how to resolve this problem.
Before that I have read some of the suggestion in this forum, and I think it is due to the later version of PHP having the global_variable = off.
After tried some of the suggested codes, but still having the same problem.

Error :
Notice: Undefined index: beef in C:\Program Files\Apache
Group\Apache2\htdocs\test\index.php on line 2

Notice: Undefined index: beef in C:\Program Files\Apache
Group\Apache2\htdocs\test\index.php on line 3

--- the code for the above is : SetCookie ("beef",$HTTP_GET_VARS['beef'],time()-3600);


Notice: Undefined variable: HTTP_SESSION_VARS in C:\Program Files\Apache
Group\Apache2\htdocs\test\top.php on line 7

Notice: Undefined variable: HTTP_SESSION_VARS in C:\Program Files\Apache> Group\Apache2

---the code for the above is: $ber=$HTTP_SESSION_VARS['bers'];

I have tried to set the global_variable to ON, and the problem still exists.
Any advise is appreciated!

Regards,
El
ghost007
Forum Commoner
Posts: 49
Joined: Sat Nov 22, 2003 10:10 am

Post by ghost007 »

hi,

You have 2 possibilities:
1)You can turn error reporting off in the php version - sometimes these warnings are run when they are not true errors, just debugging warnings.

To turn off all error_reporting use the following in the top of the php document

// Turn off all error reporting
error_reporting(0);


Howhever it's recomanded to set error reporting to 0 when launching your site it's bad practice to ignore these notification when coding your website.

It's better to:
2) first check your vars before trying to use them:

Code: Select all

<?php
if (isset($HTTP_GET_VARS['beef']))
SetCookie ("beef",$HTTP_GET_VARS['beef'],time()-3600);

if (isset($HTTP_GET_VARS['bers']))
$ber=$HTTP_SESSION_VARS['bers']; 

?>
hope this helps

Siech
Eric123
Forum Newbie
Posts: 5
Joined: Fri Dec 19, 2003 12:58 am

Post by Eric123 »

Hi,
Just another question, I noticed that these are mostly "Notices" displayed by PHP. There is a setting in php.ini

error_reporting = E_ALL & ~E_NOTICE

By suppressing the notices, would there be any problems? My Live server do not have such problems but my local development server have the notices being displayed. I think my live server having the older version of PHP than my development.

I can suppress these notices on my development server, BUT my concern is that will there be any unexpected problem I upload to the live server....
User avatar
twigletmac
Her Royal Site Adminness
Posts: 5371
Joined: Tue Apr 23, 2002 2:21 am
Location: Essex, UK

Post by twigletmac »

Don't suppress errors when developing, it isn't a solution and can hide problems that you really do need to fix (e.g. misspelled variable names). Get used to checking that variables exist before you try and use them.

You can use the ternary operator (a form of an if...else statement) to set the variable to a default if it's not set:

Code: Select all

$beef = (isset($HTTP_GET_VARS['beef'])) ? $HTTP_GET_VARS['beef'] : 'default value';

$ber = (isset($HTTP_SESSION_VARS['bers'])) ? $HTTP_SESSION_VARS['bers'] : 'default value';
Mac
Post Reply