Re: Type of form element

From: Date: Mon, 31 Oct 2005 10:38:57 +0000
Subject: Re: Type of form element
References: 1 2 3 4  Groups: php.general 
Request: Send a blank email to php-general+get-224871@lists.php.net to get a copy of this message
On 31 Oct 2005, at 06:18, Richard Lynch wrote:
But I really do believe isset($_POST['checkbox_name']) is a "good" coding practice.
OK, so PHP may not pass through unset params as NULL (it's not up to the browser), but if you don't select any checkboxes at all, the param won't exist, and asking for an index of something that doesn't exist is normally a good way to generate notices. isset's job is not to tell you if an array key exists, so why use it for that purpose? This is the difference I'm on about: $z = array('a' => 1, 'b' => NULL); echo array_key_exists('a', $z)?"yes\n":"no\n"; echo isset($z['a'])?"yes\n":"no\n"; echo array_key_exists('b', $z)?"yes\n":"no\n"; echo isset($z['b'])?"yes\n":"no\n"; This prints: yes yes yes no That last 'no' has huge bug potential. I'm not saying it doesn't have practical use, but I wouldn't call it good practice, and I wouldn't advise people to do it that way in new code. As it happens, isset _will_ usually work for things that come through the web-driven superglobals, but not all arrays come from there - if you use the same syntax for dealing with databases or your own objects you could be creating some very entertaining bugs. I don't know about you but I often deal with arrays containing NULL values where using isset would be very wrong. Marcus -- Marcus Bointon Synchromedia Limited: Putting you in the picture marcus@synchromedia.co.uk | http://www.synchromedia.co.uk

« previous php.general (#224871) next »