treehouse : what would you like to learn today?
Web Design Web Development iOS Development

PHP question.

  • What does this mean "||" in php?
  • It means OR
    $var = 1;
    if($var > 1 || $var > 0){
    echo "success";
    }
    in the example above $var is not greater than one so it fails but it then moves on to the second test, 1 is greater than 0 so it passes
  • Correct, "||" means "OR" when used in a conditional statement.

    Fun Fact: in karlpcrowly's example (and in all instances where "||" is used), if a condition ends up as TRUE, it will ignore and not solve all following conditions and simply move onto the code contained in the brackets.

    For example:
    $var = 2;
    if($var > 1 || $var > 0){
    echo "success";
    }
    Above, the first condition is TRUE so it will ignore the "$var > 0" condition and just continue on.

    So if you need to use "||" try to put the one mostly likely to be solved TRUE for first that way it only has to calculate 1 statement most of the time.

    Also, on an irrelevant note, in karlpcrowly's "if" statement, the first condition is not needed as all numbers greater than 1 are greater than 0 as well, so anything that would be TRUE for the fist statement is also TRUE for the 2nd. Just wanted to clear that up for ricky122892 so he isn't potentially confused.