Re: variable_exists() patch
| From: | nicos@php.net | Date: | Fri, 15 Aug 2003 07:41:24 +0000 |
| Subject: | Re: variable_exists() patch | ||
| References: | 1 2 3 4 5 | Groups: | php.internals |
| Request: | Send a blank email to internals+get-3985@lists.php.net to get a copy of this message | ||
"Walt Boring" <waboring@3gstech.com> a écrit dans le message de
news:3F3BDC4A.20603@3gstech.com...
> Stefan Walk wrote:
>
> >On Thu, Aug 14, 2003 at 11:15:51AM -0700, walt boring wrote:
> >
> >
> >>I for one would like to see something like variable_exists(), as I am
> >>very annoyed with
> >>the logic of isset() returning false if the variable exists and has a
> >>value of null.
> >>
> >>
> >[snip]
> >
> >
> >> I for one would much rather do
> >> if ( variable_exists($var) ) {} versus
> >>if (isset($var) || is_null($var)) {}
> >>
> >>
> >
> >When do you need to do that? I can't think of many situations where it
> >would be neccessary to check if a variable really exists.
> >
> >
> It can happen quite easily. I always develop with full warnings/errors
on.
> So if for example a var isn't set for whatever reason, then trying to
> access the
> variable will throw a php Notice. variable_exists() would prevent that,
> as does isset().
> isset() would work for my example below, but it still is a 'broken'
> function in my opinion.
>
> if ($var) { // <-- you'll get a php notice on this line
> switch ($var) {
> ...
> }
>
> }
>
> versus doing
>
> if (variable_exists($var)) { //no php notices here
> switch($var) {
> ...
> }
> }
You can avoid the notice by doing if(isset($var) && !is_null($var)) { }
We can't have a function for every piece of code.
>