Re: reason for a "Notice:.." on one site but not another? (Same code.)

From: Date: Mon, 10 Aug 2009 18:02:16 +0000
Subject: Re: reason for a "Notice:.." on one site but not another? (Same code.)
References: 1 2 3 4 5 6 7 8  Groups: php.general 
Request: Send a blank email to php-general+get-296559@lists.php.net to get a copy of this message
On Mon, Aug 10, 2009 at 1:07 PM, Martin Scotta<martinscotta@gmail.com> wrote: > Why do you all always use isset? > Why do you don't use array_key_exists instead? is it a more semantic > solution? > > <?php > > $key = 'UserWishesDateRange'; # just to make statement shorter > if( array_key_exists($key, $_POST ) && 'T' == $_POST[$key] ) > { >    echo ' the key exists... and it is a "T" ''; > } > > *isset*: Determine if a variable is set and is not NULL* > array_key_exists: *Checks if the given key or index exists in the array > > > On Mon, Aug 10, 2009 at 1:42 PM, John Butler > <govinda.webdnatalk@gmail.com>wrote: > >> >>>>  If you switch it around you'll get a notice because the IF evaluates >>> from left to right.  So you just want to make sure you check isset() >>> first. >>> >>> This would throw a notice: >>> >>> if($_POST['UserWishesDateRange']  == 'T' && >>> isset($_POST['UserWishesDateRange'])) { >>> >> >> Aha!  That must be what I tried and was still getting the notice! >>  Interesting that it works (without notice) if we check against the isset () >> one first.   It makes if() look more intelligent that I would think.... as if >> it saying, "good now that we've established that the var isset, now is it >> also equal to '___'., as opposed to just, "is var set, and is var equal to >> "___'. > > > > > -- > Martin Scotta > Two reasons: 1. isset() is orders of magnitude faster 2. The theory is, if a variable is null you don't care about it

« previous php.general (#296559) next »