Re: Warning: Undefined variable
| From: | Philip Olson | Date: | Tue, 02 Apr 2002 22:15:21 +0000 |
| Subject: | Re: Warning: Undefined variable | ||
| References: | 1 | Groups: | php.general |
| Request: | Send a blank email to php-general+get-91226@lists.php.net to get a copy of this message | ||
We are confusing one another :) I'm not talking
about declaring variables per se, but rather making
sure the variable exists before evaluating it. So:
$var = 'foobar';
if ($var == 'foobar')
That's fine, no need to do declare $var before this
in PHP. My words here simply mean to make sure it exists
before comparing it. (let's not talk about oop)
Your example of $var$i is fine, and will work without
any errors (if I'm following). Btw, you should use
arrays versus this method, arrays kick ass. Sure
doing ${'foo'.$i} or similiar has its purpose but
usually an array will do (please!).
When in doubt, put error_reporting(E_ALL) on top of your
script and go to town.
Regards,
Philip Olson
On Tue, 2 Apr 2002, Erik Price wrote:
>
> On Tuesday, April 2, 2002, at 03:27 PM, Philip Olson wrote:
>
> > If you're distributing code in cyberspace it's a good
> > idea to make it error free too, E_NOTICE or otherwise.
>
> 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 :).
>
> Sometimes I create variables dynamically, such that I could never
> declare/initialize them in any realistic sense -- for instance, I might
> have a while loop that executes a number of times equal to the number of
> rows pulled from a database query. And in this while loop, I might
> generate a new variable (assigning it a name like $variable$i, using the
> old $i++ trick to give it a unique numeric suffix).
>
> If I use this trick, does this doom me to never having fully-error-free
> code? Or is there something that I'm not getting here... The short of
> it is that, as a novice programmer, I'd like to make sure I'm writing
> the most legitimate code that I can. Thanks for any input on this
> thread, past or future.
>
>
> Erik
>
>
>
> ----
>
> Erik Price
> Web Developer Temp
> Media Lab, H.H. Brown
> pricee@hhbrown.com
>