RE: [PHP] help with undefined variables, please
| From: | Matthew Walker | Date: | Tue, 14 May 2002 18:49:56 +0000 |
| Subject: | RE: [PHP] help with undefined variables, please | ||
| Groups: | php.general | ||
| Request: | Send a blank email to php-general+get-97602@lists.php.net to get a copy of this message | ||
I prefer to use isset(), not empty(). It's more accurate, in that it
knows the difference between a variable that just isn't set, and one
that has an empty value (which may be valid).
Matthew Walker
Senior Software Engineer
ePliant Marketing
-----Original Message-----
From: Philip Olson [mailto:philip@cornado.com]
Sent: Tuesday, May 14, 2002 12:04 PM
To: The Gabster
Cc: php-general@lists.php.net
Subject: Re: [PHP] help with undefined variables, please
> PHP Notice: Undefined variable: action in
> C:\Apache\htdocs\easyletter2\easyletter.php on line 73
>
> Why do I get an undefined variable error for $action, $pw, $disp,
> $found when they do not have to be declared in the script?
> if ($action=="sign"){
At this point, $action was not set. At some point in your
script you may want to check if $action exists at all and
if not, assign it to boolean false (just one of a million
things you can do). Like, on top of the script:
// Initialize various variables if they are empty
$vars_to_init = array('action','pw','disp','found');
foreach ($vars_to_init as $var) {
if (empty(${$var})) {
${$var} = false;
}
}
That'll check them, and set empty vars to false. Seems
kinda weird, it is ;) Undefined variables are level
E_NOTICE errors in PHP. Some people find these a bit
annoying and turn them off, most prefer to eliminate
them. When you said variables don't have to be
declared in PHP, you are right in that they are just
E_NOTICE level errors, not Fatal E_ERROR. By default,
error_reporting levels do not show E_NOTICE but good
little programmers attend to them.
A quick and dirty way to magically get rid of them is
to turn down error_reporting in php.ini or at runtime.
error_reporting is both a PHP directive and a PHP
function.
There is also '@' available to suppress an error, it
works on both functions and variables. See:
http://www.php.net/manual/language.operators.errorcontrol.php
What is done of course depends on the situation and
the person.
Regards,
Philip Olson
--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php
---
Outgoing mail is certified Virus Free.
Checked by AVG anti-virus system (http://www.grisoft.com).
Version: 6.0.351 / Virus Database: 197 - Release Date: 4/19/2002