Notice: Undefined variable error

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
ljCharlie
Forum Contributor
Posts: 289
Joined: Wed May 19, 2004 8:23 am

Notice: Undefined variable error

Post by ljCharlie »

The PHP pages used to work under PHP 4.0.6 but now that we moved to PHP version 5.0.3, I'm getting the error "Notice: Undefined variable" on almost all of my PHP pages. Will anyone tell me what might be the problem here?

ljCharlie
magicrobotmonkey
Forum Regular
Posts: 888
Joined: Sun Mar 21, 2004 1:09 pm
Location: Cambridge, MA

Post by magicrobotmonkey »

its a matter of different error level reporting. Its caused by using variables without intializing them first. It can be fixed by changing you error reporting level or by going back through your code and ensuring that all variables are intialized properly.
ljCharlie
Forum Contributor
Posts: 289
Joined: Wed May 19, 2004 8:23 am

Post by ljCharlie »

Okay, so in version 5, PHP requires that I must initilalized all my variables first, correct? If so, how do I initialized in PHP then?

Many thanks for your response.

ljCharlie
magicrobotmonkey
Forum Regular
Posts: 888
Joined: Sun Mar 21, 2004 1:09 pm
Location: Cambridge, MA

Post by magicrobotmonkey »

its the same in all versions of php, the difference is that when you installed 5 it had a higher level of error reporting.

To initialize variables, you just set it equal to something. You will get the notice if you do something like $string .= "blah"; without first doing $string = "Something";
josh
DevNet Master
Posts: 4872
Joined: Wed Feb 11, 2004 3:23 pm
Location: Palm beach, Florida

Post by josh »

In php.ini set error reporting to E_ALL & ~E_NOTICE
look for a line that says:
error_reporting = E_ALL
and just make it
error_reporting = E_ALL & ~E_NOTICE
ljCharlie
Forum Contributor
Posts: 289
Joined: Wed May 19, 2004 8:23 am

Post by ljCharlie »

I see what you're saying now. Thanks! However, I have one other question. Here are some codes I have in one PHP page.

Code: Select all

echo '<form name="frmScholarship" method="post" action="'.$self.'">';
			echo '<select name="sltScholarship" id="sltScholarship" onChange="frmScholarship.submit()">';
How do I test if the variable sltScholarship is initilized or not? I'm having an error on this sltScholarship variable when coming from a differnt page to this particular page. Now, once I'm in this page I no longer receive this error because the page is submitting to self.

ljCharlie
ljCharlie
Forum Contributor
Posts: 289
Joined: Wed May 19, 2004 8:23 am

Post by ljCharlie »

Many thanks for all your help. I got it working now.

ljCharlie
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

hiding malformed code by changing error reporting isn't a good idea..
Post Reply