PHP Exercises: Check if three given numbers are in strict increasing order
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:
Flowchart:
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.
- Weekly Trends and Language Statistics
- Weekly Trends and Language Statistics