Why am i getting so many Undefined variable notices?

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
Azmerod
Forum Newbie
Posts: 5
Joined: Mon Oct 14, 2002 5:41 pm
Contact:

Why am i getting so many Undefined variable notices?

Post by Azmerod »

I have about 100 or so undefined variable notices when i try to load up my index.php file. And this is even happening with other files that have been proven to work correctly. I'm running winXP with php 4.3.2 installed and apache 1.3.
User avatar
delorian
Forum Contributor
Posts: 223
Joined: Sun May 04, 2003 5:20 pm
Location: Olsztyn, Poland

Post by delorian »

Try to use something like this at the top of your script:

Code: Select all

error_reporting(E_ALL & ~E_NOTICE);
You get "undefined variable notices", probably because of that you have error_reporting directive set to E_ALL in your php.ini file. There is a part that begins with:

Code: Select all

;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
; Error handling and logging ;
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
Set directive error_reporting to E_ALL & ~E_NOTICE and it should work fine. :D
User avatar
volka
DevNet Evangelist
Posts: 8391
Joined: Tue May 07, 2002 9:48 am
Location: Berlin, ger

Post by volka »

but check those notices first (and let error_reporting at E_ALL on your development system)
Refering to your HTTP_USER_AGENT question I'd say you try to access Get/POSTed variables (or similar) with register_globals off.
So $formVar should be something like $_POST['formVar'] and $PHP_SELF $_SERVER['PHP_SELF']
Sticky: Before Post Read: Concerning Passing Variables in PHP 4.2+ has something to say about that as has http://php.net/reserved.variables
User avatar
twigletmac
Her Royal Site Adminness
Posts: 5371
Joined: Tue Apr 23, 2002 2:21 am
Location: Essex, UK

Post by twigletmac »

delorian wrote:Try to use something like this at the top of your script:

Code: Select all

error_reporting(E_ALL & ~E_NOTICE);
You get "undefined variable notices", probably because of that you have error_reporting directive set to E_ALL in your php.ini file. There is a part that begins with:

Code: Select all

;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
; Error handling and logging ;
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
Set directive error_reporting to E_ALL & ~E_NOTICE and it should work fine. :D
To add a bit more to what volka said ('cause this is one thing that really irritates me), turning off notices does not fix the problem, it only hides it - PHP is still encountering them it's just not being reported to you. These kind of errors are generally incredibly easy to fix and having your error reporting turned up as high as possible will aid you in the long run. Notices can be incredibly useful when you mispell a variable name or try and do something with a variable that does not exist. I object heavily to 'turn down your error reporting' being presented as a solution to a problem - if you turn it off does it prevent parse errors.</objection>

Mac
owen
Forum Newbie
Posts: 15
Joined: Fri May 30, 2003 12:40 pm

in the long run

Post by owen »

in the long run defining your variables will make your programm more stall and it will be easier for you to know when something has gone horribly wrong. Also there are a few parser errors that can only be found if E_ALL is set (I don't know why exactly).
McGruff
DevNet Master
Posts: 2893
Joined: Thu Jan 30, 2003 8:26 pm
Location: Glasgow, Scotland

Post by McGruff »

I definitely agree with Twig here. Unset variables are vulnerable to value substitution - particularly if you have register globals on - so you definitely want to know about them.

Hunt them down in your scripts and work out why they are not being assigned a value.

If an undefined var is declared in an IF clause make sure there is an ELSE clause at the end to sweep up any cases not covered by the IF or ELSEIF statements.

Or, simple typos: "$site_name" declared somewhere then you refer to it later as "$sitename".

You could also give undefined vars a default value at the start of your scripts - null or something.

Of course this all refers to developer level error reporting: you'd want to log errors and use lots of "@"'s on a live site.
Post Reply