PHP - echo if two conditions are true

By IncludeHelp Last updated : January 20, 2024

Prerequisites

Problem statement

Write a PHP script that will echo some text if two conditions are true.

echo if two conditions are true

Use the PHP if statement by checking two conditions using the logical AND (&&) operator for echo if two conditions are true. Logical AND (&&) returns true if both conditions are true; false, otherwise.

PHP script for echoing if two conditions are true

Consider the below code, in this PHP code, we are checking two conditions and printing some message, if both of the conditions are true.

<?php
// taking two variables and assigning
// boolean values
$check1 = true;
$check2 = true;

// echo if two conditions are true
if ($check1 && $check2) {
    echo "Both of the given conditions are true.\n";
}

$check1 = false;
$check2 = true;

// echo if two conditions are true or false
if ($check1 && $check2) {
    echo "Both of the given conditions are true.\n";
} else {
    echo "Both of the given conditions are not true.\n";
}

// another test cases
$check1 = true;
$check2 = false;

if ($check1 && $check2) {
    echo "Both of the given conditions are true.\n";
} else {
    echo "Both of the given conditions are not true.\n";
}
?>

Output

The output of the above code is:

Both of the given conditions are true.
Both of the given conditions are not true.
Both of the given conditions are not true.

More PHP Basic Programs »

Comments and Discussions!

Load comments ↻





Copyright © 2024 www.includehelp.com. All rights reserved.