-2

What is the nature of PHP in terms of logical operators? Suppose a and b are expressions:

if (a || b) { body }

Which of the following is true?

  • PHP always evaluates truthfulness of a and b. (Oblivious nature), OR
  • PHP doesn't evaluate truthfulness of b if a is true (Adaptive nature and memory optimized: PHP evaluates truthfulness of b only if a is false).
outis
  • 75,655
  • 22
  • 151
  • 221
P K
  • 9,972
  • 12
  • 53
  • 99
  • 6
    It would take you 10 seconds to answer your own question *if only* you tested it. – user703016 Jan 07 '12 at 22:11
  • how? i can't test it. It's internals of language. – P K Jan 07 '12 at 22:11
  • This question isn't about comparisons. It's about boolean operators. – Fred Foo Jan 07 '12 at 22:12
  • no, it's all about of algorithm and dynamics of language. – P K Jan 07 '12 at 22:13
  • You can't test: `if (a() || b())` - well, then look at least into the [PHP manual](http://php.net/manual/en/language.operators.logical.php). – hakre Jan 07 '12 at 22:16
  • possible duplicate of [PHP short circuit lazy evaluation, where is it in the php.net manual?](http://stackoverflow.com/questions/3220919/php-short-circuit-lazy-evaluation-where-is-it-in-the-php-net-manual) – hakre Jan 07 '12 at 22:17
  • No, it's not. This has to do with short-circuit evaluation of the boolean (logical) operators. Are you asking if PHP somehow caches the result of the conditional? How would that ever work then, they are variables?! – Jonathon Reinhart Jan 07 '12 at 22:18
  • 4
    **How is this not testable?** An example of how to prove this is *right in the PHP manual!* – Jonathon Reinhart Jan 07 '12 at 22:19
  • Perfectly testable and googlable. – Kieran Jan 07 '12 at 22:27
  • possible duplicate of [PHP Short-Circuit Evaluation](http://stackoverflow.com/questions/5694733/php-short-circuit-evaluation) – outis Jan 07 '12 at 22:31
  • @Praveen: your use of "adaptive" and "oblivious" to describe the behavior of logical operators is not one I've seen before. Please cite your source. – outis Jan 07 '12 at 22:32
  • @outis, http://www.youtube.com/watch?v=r7bgwYkgC4c, at minute 36.45. – P K Jan 07 '12 at 22:34
  • @Praveen: In written form please, not some audio, preferable a published book. – hakre Jan 07 '12 at 22:39
  • @harke, MIT.Introduction.to.Algorithms.3rd.Edition Page 227. "An oblivious compare-exchange algorithm operates solely by a sequence of prespecified compare-exchange operations".i may be using term 'oblivious'wrongly for a simple comparison, but for sorting algortihms this type of blind comparison is told as oblivious. – P K Jan 07 '12 at 22:44
  • Wow, I still don't know what you are talking about. Compare-exchange sounds like the x86 CMPXCHG instruction? Either way I feel like you are getting topics gravely mixed up. Someone correct me if I'm mistaken. – Jonathon Reinhart Jan 08 '12 at 04:46
  • @Jonathon.. this is not a math theorem that u want me to prove, sorry i can't help you.SEARCH meaning of Oblivious nature in dictionary or read the complete book that i pointed out. – P K Jan 09 '12 at 07:11

2 Answers2

12
<?php

if (print_r("Why don't you try?") || print_r("It's not that hard")) { }

?>
user703016
  • 37,307
  • 8
  • 87
  • 112
1

This is also known as Short-circuit evaluation, which PHP uses for logical operators.

For those not wanting to click the link above, this is adapted from the PHP manual:

// --------------------
// foo() will never get called as those operators are short-circuit

$a = (false && foo());
$b = (true  || foo());
$c = (false and foo());
$d = (true  or  foo());

function foo() { echo "PHP.net Lies!"; }

There are numerous SO questions on this as well:

Also, this has negligible impact on memory usage, provided nothing is being allocated in the code that is short-circuited.

Community
  • 1
  • 1
Jonathon Reinhart
  • 132,704
  • 33
  • 254
  • 328