Announcement

Collapse
No announcement yet.

php if statement

Collapse
X
 
  • Filter
  • Time
  • Show
Clear All
new posts

  • php if statement

    just a question about the if statement of php:

    e.g.
    PHP Code:
    $a $b "b";
    if (
    $a == "a" AND $b == "b"
    now my question: when does php realize, that this statement is false? after it has checked $a or after it has checked $b or after checked both parts of the statement?

    sorry for my bad english I hope, you understand me anyway

  • #2
    ok, php is checking the statements seperatly, from left to right...
    tested with this
    PHP Code:
    <?php
    // set $a = "a"; and see the difference
    function ganz_lang() {
        
    $starttime explode(" "microtime());
        
    $starttime = (double)($starttime[0] + $starttime[1]);
        do {
            
    $endtime explode(" "microtime());
            
    $endtime = (double)($endtime[0] + $endtime[1]);
        } while ((
    $endtime $starttime) < 3);
        return 
    true;
    }

    $a "b";
    $starttime explode(" "microtime());
    $starttime = (double)($starttime[0] + $starttime[1]);
    echo 
    "start test || secs: " "<br>";

    if (
    $a == "a" AND ganz_lang()) {
        
    $tmp "";
    }
    $endtime explode(" "microtime());
    $endtime = (double)($endtime[0] + $endtime[1]);
    echo 
    "end test || secs: " . ($endtime $starttime);
    ?>

    sorry for my bad english I hope, you understand me anyway

    Comment


    • #3
      PHP Code:
      if ($foo and $bar) { 
      If $foo is false, PHP will never check $bar. Similarly:
      PHP Code:
      if ($bar and $foo) { 
      PHP will only check $foo if $bar is true. And:
      PHP Code:
      if ($bar or $foo) { 
      $foo will only be checked if $bar is not true.
      Chen Avinadav
      Better to remain silent and be thought a fool than to speak out and remove all doubt.

      גם אני מאוכזב מסיקור תחרות לתור מוטור של NRG הרשת ע"י מעריב

      Comment


      • #4
        Actually, according to my boss, if you use AND and OR, PHP will _always_ check both/all conditions in the entire IF statement before making up its mind on what to do.

        example:

        PHP Code:
        if($foo AND $bar
        PHP will check $foo, then it will check $bar, and then it will decide on whether this if statement evaluates to true or false.

        PHP Code:
        if($foo && $bar
        in this case, PHP will check if $foo is true (or set), and if not, PHP will no longer bother with checking $bar, because it knows already this statement cannot evaluate to true anymore.

        Note that I'm just conveying what my boss told me, I haven't yet verified this in the php man.

        Comment


        • #5
          No, Chen is right.

          Comment


          • #6
            KuraFire, there is no difference between AND and &&, or OR and || for the matter.
            Chen Avinadav
            Better to remain silent and be thought a fool than to speak out and remove all doubt.

            גם אני מאוכזב מסיקור תחרות לתור מוטור של NRG הרשת ע"י מעריב

            Comment


            • #7
              PHP Code:
              function foo() {
                  echo 
              'foo';
                  return 
              false;
              }
              function 
              bar() {
                  echo 
              'bar';
                  return 
              false;
              }

              if (
              foo() and bar()) {
                  echo 
              'true';

              Output: foo only.
              Chen Avinadav
              Better to remain silent and be thought a fool than to speak out and remove all doubt.

              גם אני מאוכזב מסיקור תחרות לתור מוטור של NRG הרשת ע"י מעריב

              Comment


              • #8
                hmkay, thx

                's good to finally have that answered

                Comment


                • #9
                  Apparently there IS a difference between AND and &&, or OR and ||. According to PHP's manual, AND and OR are higher precedence table, they are in the second place after the "," operator. However && and || are lower in that table, with "print", "= += -= *= /= .= %= &= |= ^= <<= >>=" and "? :" before.
                  Chen Avinadav
                  Better to remain silent and be thought a fool than to speak out and remove all doubt.

                  גם אני מאוכזב מסיקור תחרות לתור מוטור של NRG הרשת ע"י מעריב

                  Comment


                  • #10
                    Theoretically, you can use & and | (binary versions, instead of logical) to avoid the short-circuiting. This is because logical expressions will evaluate to a boolean, true/false -- 1/0. 1 & 1 = 1 (true); 1 & 0 = 0 (false); etc.

                    Comment


                    • #11
                      But... that's cheating! Good idea though, thanks for the tip.
                      Chen Avinadav
                      Better to remain silent and be thought a fool than to speak out and remove all doubt.

                      גם אני מאוכזב מסיקור תחרות לתור מוטור של NRG הרשת ע"י מעריב

                      Comment


                      • #12
                        & is also used in the bitfields, which would make sense considering that a bitfield is multiple binary bits that are set or not set !

                        Comment

                        widgetinstance 262 (Related Topics) skipped due to lack of content & hide_module_if_empty option.
                        Working...
                        X