Page 1 of 1

Defining variables important?

PostPosted: 16. January 2012 01:18
by LucenNox
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.

Re: Defining variables important?

PostPosted: 16. January 2012 06:06
by Altrea
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

Re: Defining variables important?

PostPosted: 16. January 2012 17:54
by WilliL
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";

Re: Defining variables important?

PostPosted: 16. January 2012 18:36
by Altrea
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

Re: Defining variables important?

PostPosted: 16. January 2012 20:57
by LucenNox
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.