Defining variables important?

Problems with the Windows version of XAMPP, questions, comments, and anything related.

Defining variables important?

Postby LucenNox » 16. January 2012 01:18

Hello. I'm new to XAMPP, I seem to have it mostly working. However, a number of my old programs are getting errors.
They seem to be coming from undefined variables, and variants of the theme, as best I can tell.

One of my first programs gets "Undefined index" errors, but otherwise seems to function, when using unfilled variables from a form. Other programs get undefined variable messages.

I saw that there is a setting to turn off these errors, which I'll do as long as it still shows other errors. But my real question is how important it is to define variables? Is it completely optional, optional but advised, or something that is important for some reason or another? In short, is it something I should start doing in my future programs, or something that can be left as is?

I haven't heard of any problems about this until now, but it seems pointless to have errors about something trivial.
LucenNox
 
Posts: 2
Joined: 16. January 2012 00:48
Operating System: WIN XP

Re: Defining variables important?

Postby Altrea » 16. January 2012 06:06

Hi LucenNox,

there is no correct answer for that because it is fully depending to your remaining code.
Mainly it is optional but highly recommend. In most cases PHP acts like you wished, because PHP itself has a big failure tolerance so it uses a NULL value for the undefined variables or array keys. But there are some cases where PHP acts not like you wish (maybe you want to set a default value if a variable or key is undefined). And some few cases where this can be a security issue (especially if your script depend on register_globals or you have written a function that simulate something like that).

You should clean your code from that notices. The work to do is really small and your code in your current state seems to be very sloppy to other programmers (I for example will never use scripts with such notices in production. It's simply a matter of trust).

If you have any questions or need some help feel free to ask :)

best wishes,
Altrea
We don't provide any support via personal channels like PM, email, Skype, TeamViewer!

It's like porn for programmers 8)
User avatar
Altrea
AF Moderator
 
Posts: 11926
Joined: 17. August 2009 13:05
XAMPP version: several
Operating System: Windows 11 Pro x64

Re: Defining variables important?

Postby WilliL » 16. January 2012 17:54

I saw that there is a setting to turn off these errors, which I'll do as long as it still shows other errors. But my real question is how important it is to define variables?

if I were you, i would only work with predefined variables. Then you have a definate staus when using it the first time. Debugging is much more easier when variables an mistyped for you'll get a notice.
if (!isset($vari) ? $vari = "initialize";
Willi
WilliL
 
Posts: 660
Joined: 08. January 2010 10:54
Operating System: Win7Home Prem 64 SP1

Re: Defining variables important?

Postby Altrea » 16. January 2012 18:36

WilliL wrote:if (!isset($vari) ? $vari = "initialize";

Small correction:

either:
Code: Select all
$var1 = isset( $var1 )
      ? $var1
      : 'initialize';

or:
Code: Select all
if( !isset( $var1 ) ) {
    $var1 = 'initialize';
}


best wishes,
Altrea
We don't provide any support via personal channels like PM, email, Skype, TeamViewer!

It's like porn for programmers 8)
User avatar
Altrea
AF Moderator
 
Posts: 11926
Joined: 17. August 2009 13:05
XAMPP version: several
Operating System: Windows 11 Pro x64

Re: Defining variables important?

Postby LucenNox » 16. January 2012 20:57

Alright. Thanks, Altrea. Shouldn't be too much of a hassle to start doing that.

WilliL wrote: Debugging is much more easier when variables an mistyped for you'll get a notice.

Ah yes. I'm usually pretty good with keeping my variables straight, but I remember well having to hunt through multiple files tracking down an error in classmates' programs. I suppose now I have no choice but to fix all my files from learning to keep my inner data neat-freak happy. At least anything complex uses SQL, so that's already defined everywhere.
LucenNox
 
Posts: 2
Joined: 16. January 2012 00:48
Operating System: WIN XP


Return to XAMPP for Windows

Who is online

Users browsing this forum: No registered users and 100 guests