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
Nunners
Forum Commoner
Posts: 89
Joined: Tue Jan 28, 2003 7:52 am
Location: Worcester, UK
Contact:

Undefined variables

Post by Nunners »

Before anyone says anything, I've read through this whole thread, and another couple about poassing Post & GET variables... however no-one has ever mentioned what you do if you have an undefined variable...

For example:
I've created what I call modules, and depending on the _Post variable $command it includes a specific file, which does an action. However, if you are opening the "page" for the first time, the command varilable is purposefully blank.

This worked under the previous php versions, however with register globals in the new setting, is says that the index is undefined.

Is there a way around this? Please!
User avatar
twigletmac
Her Royal Site Adminness
Posts: 5371
Joined: Tue Apr 23, 2002 2:21 am
Location: Essex, UK

Post by twigletmac »

[Moved this from: viewtopic.php?t=511]

Undefined variable errors are caused when you try and access a variable that has not been set, for example if you had:

Code: Select all

if ($_GET['action'] == 'go') {
and $_GET['action'] was not set you would get a notice about an undefined variable.

To avoid these notices, start using empty() and isset(), so you could change the above to:

Code: Select all

if (!empty($_GET['action']) && $_GET['action'] == 'go') {
or if you had

Code: Select all

if ($_GET['action']) {
you could change it to

Code: Select all

if (empty(!$_GET['action'])) {
of course if you post some of the code that you're having problems with I could give a more specific example.

BTW, the reason you didn't get the error (notice) in your previous version of PHP was because error reporting was set lower - some people will tell you that the solution to getting rid of errors like this you should just lower the error reporting level but this isn't a solution, the error still occurs but it's just not reported.

Mac
Nunners
Forum Commoner
Posts: 89
Joined: Tue Jan 28, 2003 7:52 am
Location: Worcester, UK
Contact:

Post by Nunners »

Thanks for that Mac.... I've played around with it a bit... and indeed the isset seems the best way forward... just means there's another thing to remember to do!!!
Post Reply