Logical Operators In Php

From The Below Example We Will Get To Know Basics Of logical Operator.

<?php

$num1 = 10;

$num2 = 5;

// And (&&)

$result = ($num1 > 0) && ($num2 > 0);

echo $result . "\n"; // Output: 1 (true)

// Or (||)

$result = ($num1 > 0) || ($num2 < 0);

echo $result . "\n"; // Output: 1 (true)

// Not (!)

$result = !($num1 > 0);

echo $result . "\n"; // Output: 0 (false)

?>

 

In this example, we first declare two variables $num1 and $num2 and assign them values. 

Then we use the logical operators &&, ||, and ! to perform logical operations and evaluate conditions. The result of each operation is stored in the $result variable and then printed to the screen using the echo statement.

The && operator returns true if both conditions are true, while the || operator returns true if either condition is true. 

The ! operator reverses the truth value of a condition, so it returns true if the condition is false and false if the condition is true.

Comments