Forum Moderators: coopster

Message Too Old, No Replies

Variable initialization

Is it really necessary

         

andrewheiss

8:58 pm on Jul 16, 2008 (gmt 0)

10+ Year Member



I was messing with error reporting levels and decided to enable every possible error and notice when I discovered to my horror that my page, which works perfectly, was riddled with "Undefined variable" notices.

I've done some research into the pros and cons of initializing every variable and it seems like it's only an issue when REGISTER_GLOBALS is turned on. If it's off, do I really need to start every script with $var1 = "", $var 2 = "" and so on?

What is the best practice?

FourDegreez

9:13 pm on Jul 16, 2008 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



I'd like to know, too. Coming from a java background, I cringe at code that doesn't initialize variables (such code likely wouldn't compile in java). But I realize PHP doesn't require it. So I do it most times, but sometimes I'm lazy and don't. I'm also wondering what the "right way" is.

MattAU

10:58 pm on Jul 16, 2008 (gmt 0)

10+ Year Member



I think one of PHP's strengths (an weaknesses) is that you don't need to waste time worrying about initialising pesky variables at the start.

However, getting undefined variable notices is usually a symptom of a different problem - not great code. Whilst they don't all need be initialised at the start (or anywhere), you should really be using isset type functions instead of turning errors off. You may not be getting the results you're expecting if you're not doing error checking and correcting yourself.

andrewheiss

11:21 pm on Jul 16, 2008 (gmt 0)

10+ Year Member



So it's better to use isset() rather than checking if a variable is null? For example:


if (isset($_POST['thing'])) {
// Code goes here
}

is better than

if ($_POST['thing'] != "") {
// Code goes here
}

?

MattAU

11:30 pm on Jul 16, 2008 (gmt 0)

10+ Year Member



Definitely. Or you could use empty().

andrewheiss

12:42 am on Jul 17, 2008 (gmt 0)

10+ Year Member



Awesome - I didn't know empty() existed. I was using $thing = "" because sometimes my $_POST variables would get sent as null/empty even if they weren't officially supposed to be submitted. empty() seems to do the trick. Has anyone else come across having $_POST variables set but empty for some reason?

PHP_Chimp

7:42 am on Jul 17, 2008 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Make sure that you use isset and empty to check for the correct things.
As isset checks to see if the variable...is set. An empty string is set, just empty. The only things that are not set is variable that have been unset [uk3.php.net] or null (that is PHP null not a null byte).
empty checks for:
* "" (an empty string)
* 0 (0 as an integer)
* "0" (0 as a string)
* NULL
* FALSE
* array() (an empty array)
* var $var; (a variable declared, but without a value in a class)

So isset and empty do different things. Notice that the string 0 is empty. So if you are expecting a numeric argument (say to a question like "how many children do you have?" then an answer of 0 would be acceptable...but empty). So you need to make sure that you use the functions in the correct place.

So you may actually want to use both. Using isset to check that all of the fields you expect have been submitted, then use empty to check that people have actually put some data into those fields.

Although of course you are validating every user supplied bit of data...aren't you?
So empty may become part of that check, along with something like a regex or any of the is_something functions (like is_numeric [uk3.php.net])

[edited by: PHP_Chimp at 7:43 am (utc) on July 17, 2008]

andrewheiss

2:04 pm on Jul 17, 2008 (gmt 0)

10+ Year Member



OK, that makes more sense. Yes, I'm validating and cleaning all the user supplied data. So using if (isset() && !empty()) {} seems to be the way to go.