note 66233 added to language.operators.logical

From: Date: Wed, 17 May 2006 12:33:03 +0000
Subject: note 66233 added to language.operators.logical
Groups: php.notes 
Request: Send a blank email to php-notes+get-111490@lists.php.net to get a copy of this message
IF blocks are much more reliable. Using OR in place of IF lead to unexpected results and has limited functionality. If you must short-hand it, do this: <? valid code; if (!$i = give_me_liberty()) die("FATAL DB ERROR!"); // There's only 3 more characters in this line, than in the one below. valid code; ?> And don't ever allow your functions to return 0 on success. Quote: Serafina 25-Jun-2005 10:06 Wrote: > Because the OR shorthand for an if block can produce more > readable code with less typing, it is tempting to produce functions that will return FALSE on > failure or some other data type on success. Like mysql_connect, which "Returns a MySQL link > identifier on success, or FALSE on failure." > > The novice php php developer should avoid creating functions which might produce a FALSE on > failure and an integer on success, if there is any chance that the integer might be zero. > > <?php > blah blah blah; > $i = give_me_liberty() or die("FATAL DB ERROR!"); > blah blah blah; > ?> > > If the function give_me_liberty() occasionally completes successfully and produces a zero, the > result could be perplexing. ---- Server IP: 64.71.164.2 Probable Submitter: 65.100.194.45 ---- X-Spam-Status: No, hits=3.1 required=5.0 tests=DATE_MISSING,FROM_NO_LOWER autolearn=no version=2.64 ---- Manual Page -- http://www.php.net/manual/en/language.operators.logical.php Edit -- http://master.php.net/note/edit/66233 Del: integrated -- http://master.php.net/note/delete/66233/integrated Del: useless -- http://master.php.net/note/delete/66233/useless Del: bad code -- http://master.php.net/note/delete/66233/bad+code Del: spam -- http://master.php.net/note/delete/66233/spam Del: non-english -- http://master.php.net/note/delete/66233/non-english Del: in docs -- http://master.php.net/note/delete/66233/in+docs Del: other reasons-- http://master.php.net/note/delete/66233 Reject -- http://master.php.net/note/reject/66233 Search -- http://master.php.net/manage/user-notes.php

« previous php.notes (#111490) next »