On 08 October 2003 16:13, Chris Shiflett contributed these pearls of wisdom:
> --- "Ford, Mike [LSS]" <[EMAIL PROTECTED]>" <[EMAIL PROTECTED]> > wrote: >> Firstly, "or", as a Boolean operator requires two operands, >> both of which must have an actual value. > > [snip] > >> statements don't have a value (and can't even be coerced to >> have one), so "return" can't be valid as one of the operands >> to "or". > > Based on yesterday's discussion, this seems to be a very > common misconception. No, it's not -- the misconception appears to be yours. > I will try to clear it up. And I will try to clear up your clearing up. > > Let's take a common use: > > mysql_query($sql) or die(mysql_error()); > > There is a conditional here, but it has nothing to do with > the die() part. The > "or" is not the same as "||" in a conditional expression. Only because it's got a different precedence -- in all other respects, || and or are identical (Boolean-or operator with lazy, or "short-circuit", evaluation). > The > return of die() is not being evaluated. That's because of the lazy evaluation of Boolean operators in PHP, nothing to do with whether it's "or" or ||. > Only the return of > mysql_query() is. > This can be > rewritten as follows: > > if (!mysql_query($sql)) > { > die(mysql_error()); > } > > If it helps, you can possibly replace "or" with "else" when > you read these > types of statements to clarify the use. It makes sense to me > like it is. > > This really just boils down to language semantics, and it > probably makes more > sense to native English speakers than others. The word "or" > means different > things in the following two examples: > > 1. If you find some Coke or Pepsi, buy some. > > This suggests that either Coke or Pepsi will suffice. In PHP: > > if ($type == 'coke' || $type == 'pepsi') OR: if ($type == 'coke' or $type == 'pepsi') (Note that lazy evaluation applies in this case as well -- if $type is, in fact, == 'coke', the $type == 'pepsi' condition will never be evaluated.) > { > buy_some(); > } > > 2. Bring me the antidote, or I will die. > > This suggests that if the antidote is not brought, the > speaker will die. If it > is, the "or I will die" part never happens. In PHP: > > bring_antidote() or die; OR: bring_antidote() || die; There really is no difference between the "or" and || operators apart from their precedence (see, for example, http://uk.php.net/manual/en/language.operators.php#language.operators.preced ence and http://uk.php.net/manual/en/language.operators.logical.php), and because of their ordering in relation to other operators it's really only in combination with assignment operators that they can cause problems. For example, consider the following: $y = validate($x) or print "Invalid!"; This is evaluated as ($y = validate($x)) or print "Invalid!"; which is probably what was intended -- $y is assigned the the value of validate($x), and then the print is only executed if that is false. However: $y = validate($x) || print "Invalid!"; is evaluated as: $y = (validate($x) || print "Invalid!"); which will assign to $y the value of validate($x) if it is non-empty, otherwise will execute the print and assign 1 (the return value of print) to $y. This is probably not what the writer intended. Cheers! Mike --------------------------------------------------------------------- Mike Ford, Electronic Information Services Adviser, Learning Support Services, Learning & Information Services, JG125, James Graham Building, Leeds Metropolitan University, Beckett Park, LEEDS, LS6 3QS, United Kingdom Email: [EMAIL PROTECTED] Tel: +44 113 283 2600 extn 4730 Fax: +44 113 283 3211 -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php