note 66233 deleted from language.operators.logical by nlopess

From: Date: Fri, 02 Jun 2006 08:55:21 +0000
Subject: note 66233 deleted from language.operators.logical by nlopess
References: 1  Groups: php.notes 
Request: Send a blank email to php-notes+get-112911@lists.php.net to get a copy of this message
Note Submitter: egingell at sisna dot com ---- 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.

« previous php.notes (#112911) next »