Can you use or in php?

There's some joking, and misleading comments, even partially incorrect information in the answers here. I'd like to try to improve on them:

First, as some have pointed out, you have a bug in your code that relates to the question:

if ($status = 'clear' AND $pRent == 0)

should be (note the == instead of = in the first part):

if ($status == 'clear' AND $pRent == 0)

which in this case is functionally equivalent to

if ($status == 'clear' && $pRent == 0)

Second, note that these operators (and or && ||) are short-circuit operators. That means if the answer can be determined with certainty from the first expression, the second one is never evaluated. Again this doesn't matter for your debugged line above, but it is extremely important when you are combining these operators with assignments, because

Third, the real difference between and or and && || is their operator precedence. Specifically the importance is that && || have higher precedence than the assignment operators (= += -= *= **= /= .= %= &= |= ^= <<= >>=) while and or have lower precendence than the assignment operators. Thus in a statement that combines the use of assignment and logical evaluation it matters which one you choose.

Modified examples from PHP's page on logical operators:

$e = false || true;

will evaluate to true and assign that value to $e, because || has higher operator precedence than =, and therefore it essentially evaluates like this:

$e = (false || true);

however

$e = false or true;

will assign false to $e (and then perform the or operation and evaluate true) because = has higher operator precedence than or, essentially evaluating like this:

($e = false) or true;

The fact that this ambiguity even exists makes a lot of programmers just always use && || and then everything works clearly as one would expect in a language like C, ie. logical operations first, then assignment.

Some languages like Perl use this kind of construct frequently in a format similar to this:

$connection = database_connect($parameters) or die("Unable to connect to DB.");

This would theoretically assign the database connection to $connection, or if that failed (and we're assuming here the function would return something that evalues to false in that case), it will end the script with an error message. Because of short-circuiting, if the database connection succeeds, the die() is never evaluated.

Some languages that allow for this construct straight out forbid assignments in conditional/logical statements (like Python) to remove the amiguity the other way round.

PHP went with allowing both, so you just have to learn about your two options once and then code how you'd like, but hopefully you'll be consistent one way or another.

Whenever in doubt, just throw in an extra set of parenthesis, which removes all ambiguity. These will always be the same:

$e = (false || true);
$e = (false or true);

Armed with all that knowledge, I prefer using and or because I feel that it makes the code more readable. I just have a rule not to combine assignments with logical evaluations. But at that point it's just a preference, and consistency matters a lot more here than which side you choose.

PHP If OR

PHP If condition can be compound condition. So, we can join multiple simple conditions with logical OR operator and use it as condition for PHP If statement.

In this tutorial, we will go through the syntax of using OR operator in If-statement and some examples to understand the usage.

Syntax – PHP If with OR operator

The syntax of If-statement with OR logical operator is

if ( condition_1 || condition_2 ) {
  //if-block statement(s)
}

where

  • condition_1 and condition_2 can be simple conditional expressions or compound conditional expressions.
  • || is the logical OR operator in PHP. It takes two operands: condition_1 and condition_2.

Since we are using OR operator to combine the condition, PHP executes if-block if at least one of the condition_1 and condition_2 is true. If both the conditions are false, then PHP does not execute if-block statement(s).

Example – PHP If with OR Operator

In this example, we will write an if statement with compound condition. The compound condition contains two simple conditions and these are joined by OR logical operator.

PHP Program

<?php
$a = 2;
$b = 8;

if ( ( $a == 2 ) || ( $b == 5 ) ) {
    echo "a is 2 or b is 5.";
}
?>

Output

Can you use or in php?

Conclusion

In this PHP Tutorial, we learned how to write PHP If statement with AND logical operator.

Can you use or in an if statement PHP?

PHP If OR. PHP If condition can be compound condition. So, we can join multiple simple conditions with logical OR operator and use it as condition for PHP If statement.

What does || mean in programming?

Logical OR operator: || The logical OR operator ( || ) returns the boolean value true if either or both operands is true and returns false otherwise. The operands are implicitly converted to type bool before evaluation, and the result is of type bool . Logical OR has left-to-right associativity.

What is XOR operator in PHP?

PHP Xor Operator In PHP, the logical operator xor stands for exclusive or. It takes two different boolean values or expressions as its operands and returns a single boolean value. xor evaluates to TRUE only if either its left operand or its right operand evaluate to TRUE , but not both.

What is the => operator in PHP?

What is => in PHP? This is referred to as the double arrow operator. It is an assignment operator used in the creation of associative arrays. It is placed in between the array key and its value.

Tải thêm tài liệu liên quan đến bài viết Can you use or in php?