Re: Warning: Undefined variable
| From: | Rasmus Lerdorf | Date: | Wed, 03 Apr 2002 13:05:35 +0000 |
| Subject: | Re: Warning: Undefined variable | ||
| References: | 1 | Groups: | php.general |
| Request: | Send a blank email to php-general+get-91297@lists.php.net to get a copy of this message | ||
> I got back quite a few responses on this list on the subject of having
> variables that are not defined before they are used. That's
> interesting -- it's nice to know that I can take shortcuts and conjure
> them up on the fly, but I didn't realize that it was good practice to
> declare the variable before you actually use it in a script. (I never
> studied programming in any formal sense, as anyone who has seen my
> source code can attest :).
You don't need to declare variables, simply initialize them before using
them in a way that wouldn't auto-initialize.
For example:
$foo = $i++;
In this case $foo is auto-initialized, but you are incrementing $i without
explicitly initializing it to 0. This code should be:
$i = 0;
$foo = $i++;
It is common sense really.
-Rasmus