w3resource

PHP Exercises: Check if three given numbers are in strict increasing order


48. Strict Increasing Order, Allow Equality Option

Write a PHP program to check if three given numbers are in strict increasing order, such as 4 7 15, or 45, 56, 67, but not 4 ,5, 8 or 6, 6, 8.However,if a fourth parameter is true, equality is allowed, such as 6, 6, 8 or 7, 7, 7.

Sample Solution:

PHP Code :

<?php
// Define a function that checks the order of $x, $y, and $z based on the value of $flag
function test($x, $y, $z, $flag)
{
    // If $flag is true, check if $x is less than or equal to $y and $y is less than or equal to $z
    // If $flag is false, check if $x is less than $y and $y is less than $z
    return $flag ? $x <= $y && $y <= $z : $x < $y && $y < $z;
}

// Test the function with different sets of numbers and flags
var_dump(test(1, 2, 3, false))."\n";
var_dump(test(1, 2, 3, true))."\n";
var_dump(test(10, 2, 30, false))."\n";
var_dump(test(10, 10, 30, true))."\n";
?>

Explanation:

  • Function Definition:
    • The function test checks the order of three numbers $x, $y, and $z based on the value of a boolean parameter $flag.
  • Condition Checked:
    • If $flag is true: The function checks if $x is less than or equal to $y and $y is less than or equal to $z ($x <= $y && $y <= $z).
    • If $flag is false: The function checks if $x is strictly less than $y and $y is strictly less than $z ($x < $y && $y &lr $z).

Output:

bool(true)
bool(true)
bool(false)
bool(true)

Visual Presentation:

PHP Basic Algorithm Exercises: Check if three given numbers are in strict increasing order.

Flowchart:

Flowchart: Check if three given numbers are in strict increasing order.

For more Practice: Solve these Related Problems:

  • Write a PHP script to check if three numbers are in strict increasing order, but allow equal values if a fourth parameter is true.
  • Write a PHP function to test for an increasing sequence with an optional equality flag to permit adjacent equal numbers.
  • Write a PHP program to conditionally check the ordering of three values based on a boolean parameter that enables equality.
  • Write a PHP script to implement a custom comparison that verifies order among three integers with optional non-strict conditions when specified.

PHP Code Editor:



Contribute your code and comments through Disqus.

Previous: Write a PHP program to check if it is possible to add two integers to get the third integer from three given integers.
Next: Write a PHP program to check if two or more non-negative given integers have the same rightmost digit.

What is the difficulty level of this exercise?

Test your Programming skills with w3resource's quiz.



Follow us on Facebook and Twitter for latest update.