DEV Community πŸ‘©β€πŸ’»πŸ‘¨β€πŸ’»

Patricia Hernandez
Patricia Hernandez

Posted on

PHP OR Operator (||)

The PHP or operator used to check for only one true result of many expressions So that means only one operand should has a true result to show you a true result value. Otherwise it will show you a false Boolean value.

Lets take an example.

<?php
  $val = 52 || 20;
  Var_dump($val); // true
?>
Enter fullscreen mode Exit fullscreen mode

This example will show you a true value because any positive or negative number means it has a value but if it has 0 it will show you a false value.

That means the interpreter will use the PHP type juggling to convert the integer value to Boolean value during the execution, so, the true take any positive or negative value and 0 means false Boolean value.

To analyze this example the 52 is not zero number and not empty.

The value will be true and the 20 value will be the same result as a true Boolean value so the final result will be true.

In in the next example will use another integer expression.

PHP OR with Integer Data Types

<?php
  $x = -10 || 0;
  var_dump($x); // true;
?>
Enter fullscreen mode Exit fullscreen mode

In this example, you will see two values with OR logical operator, as I mentioned before, the full result will determined according to true boolean operand.

So -10 means not empty and not zero value that will convert the integer type to true and 0 converted to false value.

The final result will true.

PHP OR with a String and NULL Data Types

Anyway, in the following block I will use the same operator with another string data type.

<?php
  $string = null || "";
  var_dump($string); // false   

?>
Enter fullscreen mode Exit fullscreen mode

These two operands will return a false value which will return a false boolean value.

Anyway, there are two empty values so it will show you a false boolean value.

<?php
  $value =  "value 1" || null;
  var_dump($value); // true;
?>
Enter fullscreen mode Exit fullscreen mode

In this example I will show you a true boolean value because there is an operand has no empty value.

Wrapping Up

PHP OR operator is a logical operator used to check if one operand has a true value so it will show you a true boolean result.

That’s all, thank you for reading.

Resources

CodedTag

Top comments (0)

Find what you were looking for? Sign up so you can:

🌚 Enable dark mode
πŸ”  Change your default font
πŸ“š Adjust your experience level to see more relevant content