Treehouse: Grow your CSS skills. Land your dream job.

PHP question.

  • # April 2, 2012 at 5:21 pm

    What does this mean “||” in php?

    # April 2, 2012 at 5:27 pm

    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

    # April 2, 2012 at 6:04 pm

    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.

Viewing 3 posts - 1 through 3 (of 3 total)

You must be logged in to reply to this topic.