Forum Moderators: coopster
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?
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.
* "" (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 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]