The PHP AND (&&) operator is one of the PHP logical operators. Used to return a true boolean value if all compared values have true boolean results.
Before getting started, you have to read more about the PHP data types tutorial and the type juggling.
The AND (&&) operator can be written as “&&” or AND between the compared values or variables. For example.
<?php
var_dump( true && true ); // bool(true)
?>
So, the AND (&&) Operator returns the true value if all parts have a correct value. Let’s see the && operator with all PHP data types and control structures.
Use the PHP AND (&&) Operator with Conditions
The logical operators are designed to check about a correct part, one part, or not equal part using the block condition.
In this section, you will understand how can we use the AND (&&) Operator with inline if and if statements.
Use the AND (&&) operator with the if condition.
<?php
$a = 5;
$b = 16;
$c = 100;
if ( $a == 5 && $b == 16 && $c == 100 ) {
echo "Right Values";
}
echo "\n";
if ( $a == 5 AND $b == 16 AND $c == 100 ) {
echo "Right Values";
}
?>
PHP AND (&&) operator with the inline condition.
<?php
$vars = ( 10 < 100 && 10 == "10" ) ? "Right Values" : "Not Right";
echo $vars;
?>
Use the AND (&&) in the Variable Directly
You can assign the && operator to the PHP variable directly. And here the PHP juggling does a casting for other types to the boolean data type.
Check the below example.
<?php
$value = true && false;
var_dump($value); // bool(false)
?>
The previous example shows you a false boolean result because there is one part that has a false. And that will not work with the AND operator. The reason is AND operator only detecting for true boolean parts.
So, for another example.
<?php
$value = true && true;
var_dump($value); // bool(true)
?>
It can also work with AND keyword. For example.
<?php
$value = true AND true;
var_dump($value); // bool(true)
?>
That’s all, thank you for reading. Stay tuned for our new articles.