php echo if two conditions are true
You can use the if statement in PHP to check if two conditions are true.
Tags
You can use the if statement in PHP to check if two conditions are true. The syntax for an if statement is as follows:
Example of an if statement in PHP to echo if two conditions are true
<?php
$condition1 = true;
$condition2 = true;
if ($condition1 && $condition2) {
echo "Both conditions are true\n";
}
$condition1 = false;
$condition2 = true;
if ($condition1 && $condition2) {
echo "Both conditions are true\n";
} else {
echo "One of the conditions is false\n";
}
$condition1 = true;
$condition2 = false;
if ($condition1 && $condition2) {
echo "Both conditions are true\n";
} else {
echo "One of the conditions is false\n";
}
$condition1 = false;
$condition2 = false;
if ($condition1 && $condition2) {
echo "Both conditions are true\n";
} else {
echo "One of the conditions is false\n";
}
<div class="alert alert-info flex not-prose">![]()
<span class="hidden md:block">Watch a video course</span>Learn object oriented PHP</div>
You can use && operator to check multiple conditions.
Example of using && operator to check multiple conditions
<?php
$a = 10;
$b = 20;
if ($a > 5 && $b < 30) {
echo "Both conditions are true\n";
}
$a = 5;
$b = 20;
if ($a > 5 && $b < 30) {
echo "Both conditions are true\n";
} else {
echo "One of the conditions is false\n";
}
$a = 10;
$b = 35;
if ($a > 5 && $b < 30) {
echo "Both conditions are true\n";
} else {
echo "One of the conditions is false\n";
}
$a = 5;
$b = 35;
if ($a > 5 && $b < 30) {
echo "Both conditions are true\n";
} else {
echo "One of the conditions is false\n";
}In this example, the code inside the if statement will be executed if the value of $a is greater than 5 and the value of $b is less than 30.