PHP's 'or' operator

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • Xx r3negade
    New Member
    • Apr 2008
    • 39

    #1

    PHP's 'or' operator

    I've seen before in scripts something like this:
    Code:
    doSomething() or doSomethingElse()
    So in other words, if doSomething() fails, doSomethingElse () will execute.
    I've tried to do the same thing:
    Code:
    <?php
    function doSomething($x)
    {
      if ( $x != 0 )
      {
        throw new Exception('error!');
      }
    }
    function doSomethingElse()
    {
      print 'something else';
    }
    doSomething(0) or doSomethingElse();
    ?>
    The problem is, it *always* executes doSomethingElse (), regardless of whether an exception was thrown or not. Any help?

    And yes, I know about "try...catc h"; I just like to have a one-liner alternative.
  • MarkSponge
    New Member
    • Apr 2008
    • 6

    #2
    If you make the first function return a bool then it will only call the second function after the 'or' if it returns false.

    If show is passed a 1 - "number is 1" is echoed.
    If show is passed <> 1 - "number not 1" is echoed.

    Code:
    <?php
      function show($x){
        if($x==1){
          echo "number is 1";
          return true;
        }else{
          return false;
        }
      }
    
      function show2(){
        echo "number not 1";
      }
      
      show(1) or show2();
    ?>

    Comment

    • TheServant
      Recognized Expert Top Contributor
      • Feb 2008
      • 1168

      #3
      Originally posted by Xx r3negade
      I've seen before in scripts something like this:
      Code:
      doSomething() or doSomethingElse()
      So in other words, if doSomething() fails, doSomethingElse () will execute.
      I've tried to do the same thing:
      Code:
      <?php
      function doSomething($x)
      {
        if ( $x != 0 )
        {
          throw new Exception('error!');
        }
      }
      function doSomethingElse()
      {
        print 'something else';
      }
      doSomething(0) or doSomethingElse();
      ?>
      The problem is, it *always* executes doSomethingElse (), regardless of whether an exception was thrown or not. Any help?

      And yes, I know about "try...catc h"; I just like to have a one-liner alternative.
      What about just doing:
      [PHP]if ($x != 0)
      {
      throw new Exception('erro r!');
      } else {
      print 'something else';
      }
      [/PHP]

      When you wirte doSomething(0) or doSomethingElse (); it makes no sense to the computer. It doesn't know which to choose because you have not given it any conditions. You need to say IF (this) {dothis} ELSEIF (that) {dothat}. You use OR like so: if (x ==1 OR x==0) {dothis} That's the logic anyway.

      Comment

      • Motoma
        Recognized Expert Specialist
        • Jan 2007
        • 3236

        #4
        Hi Xx r3negade,
        MarkSponge is right on the money: The return value of your function is what makes the different. PHP utilizes a programming concept called "Short Circuit Evaluation." The return values from your functions are used to determine if the program continues to call functions or quit.

        My apologies, I feel that I am inarticulate tonight, so I will show some code!

        [CODE=php]
        <?php

        function returnTrue()
        {
        echo 'returnTrue called!<br />';
        return true;
        }

        function returnFalse()
        {
        echo 'returnFalse called!<br />';
        return false;
        }

        echo 'returnTrue() or returnFalse();< hr />'
        returnTrue() or returnFalse();
        echo '<hr />';

        echo 'returnFalse() or returnTrue();<h r />'
        returnFalse() or returnTrue();
        echo '<hr />';


        echo 'returnTrue() and returnFalse();< hr />'
        returnTrue() and returnFalse();
        echo '<hr />';

        echo 'returnFalse() and returnTrue();<h r />'
        returnFalse() and returnTrue();
        echo '<hr />';

        ?>
        [/CODE]

        I hope that helps you out,
        Motoma

        Originally posted by Xx r3negade
        I've seen before in scripts something like this:
        Code:
        doSomething() or doSomethingElse()
        So in other words, if doSomething() fails, doSomethingElse () will execute.
        I've tried to do the same thing:
        Code:
        <?php
        function doSomething($x)
        {
          if ( $x != 0 )
          {
            throw new Exception('error!');
          }
        }
        function doSomethingElse()
        {
          print 'something else';
        }
        doSomething(0) or doSomethingElse();
        ?>
        The problem is, it *always* executes doSomethingElse (), regardless of whether an exception was thrown or not. Any help?

        And yes, I know about "try...catc h"; I just like to have a one-liner alternative.

        Comment

        Working...