How the PHP or (||) Operator Works

Actually, PHP or (||) is a logical operator used to give you a true result if one of operands is correct.

The syntax of or operator like the below .

<?php 
operand_1 || operand_2

// => or

operand_1 or operand_2
?>

So when you use it with if condition it would like the below.

<?php 
$oper1 = true;
$oper2=false;
$oper3=false;

$value = $oper1 || $oper2 || $oper3;

var_dump($value); // Bool( true )
?>

The result will be a true because there is a correct boolean result in the three operands.

Examples

<?php

$is_loggedin = false;
$username = "saly";
$pass = "arkedia"
if( $username != 'saly' || $pass != 'arkedia' ) {
  $is_loggedin = false;
} else {
  echo "Hello, How are you {$username}";
}

?>

In this example, if the $username or $pass are not correct according to the user inputs it will show false boolean value which mean the condition show you a warning regarding the user details.

So the PHP or operator checks for only one operand has not correct information.

For another example.

<?php

$string = "Welcome to PHP tutorials";

if( strlen( $string  ) > 5 or strpos( $string, "PHP" ) !== false ) {
  echo "This is a new PHP tutorial";
}

?>

This condition will be achieved which means the string is more than 5 length and it is including "PHP" string text so the result would be like this in condition head ( true or true )

Conclusion

The PHP or ( || ) operator used to check if one expression of many operand has a true logical boolean value so it will execute the condition otherwise it will show you a false boolean to allow the condition execute the else statements.

thank you for reading

Coded Tag