PHP - echo if either condition is true

By IncludeHelp Last updated : January 20, 2024

Prerequisites

Problem statement

Write a PHP script that will echo some text if either condition is true.

echo if either condition is true

Use the PHP if statement by checking two or more conditions using the logical OR (||) operator for echoing if either condition is true. The logical OR (||) returns true if either condition is true; false, if all the given conditions are false.

PHP script for echoing if either condition is true

Consider the below code, in this PHP code, we are checking two conditions and printing some messages if ether condition is true.

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

// echo if either condition is true
if ($check1 || $check2) {
  echo "One or both conditions is/are true.\n";
}

$check1 = false;
$check2 = false;

// echo if either condition is true
if ($check1 || $check2) {
  echo "One or both conditions is/are true.\n";
} else {
  echo "Both of the conditions are false.\n";
}

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

if ($check1 || $check2) {
  echo "One or both conditions is/are true.\n";
} else {
  echo "Both of the conditions are false.\n";
}

Output

The output of the above code is:

One or both conditions is/are true.
Both of the conditions are false.
One or both conditions is/are true.

More PHP Basic Programs »

Comments and Discussions!

Load comments ↻





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