Home > Backend Development > PHP Problem > Find maximum value from two positive integer values ​​within specified range via PHP

Find maximum value from two positive integer values ​​within specified range via PHP

藏色散人
Release: 2023-03-12 07:14:02
Original
2191 people have browsed it

In the previous article "Copy a given string and output a new string through PHP", I introduced you how to copy a given string through PHP and output a new string. Strings, interested friends may wish to take a look ~

Then the theme of this article is "Finding the maximum value from two positive integer values ​​within a specified range through PHP".

Let's expand on this question: "How to write a PHP program to find the larger value from two positive integer values ​​in the range of 20 to 30. If both are not in this range, then Return 0".

You can practice locally and see if you can write this PHP program that meets the requirements!

The following is the method I gave. I don’t know if it is the same as what you think~

The PHP code is as follows:

<?php
function test($x, $y)
{
    if ($x >= 20 && $x <= 30 && $y >= 20 && $y <= 30)
    {
        if ($x >= $y)
        {
            return $x;
        }
        else
        {
            return $y;
        }
    }
    else if ($x >= 20 && $y <= 30)
    {
        return $x;
    }
    else if ($y >= 20 && $y <= 30)
    {
        return $y;
    }
    else
    {
        return 0;
    }
}


echo test(78, 95)."<br>";
echo test(20, 30)."<br>";
echo test(21, 25)."<br>";
echo test(28, 28)."<br>";
Copy after login

Output Result:

Find maximum value from two positive integer values ​​within specified range via PHP

0
30
25
28
Copy after login

In the above code, we mainly implement it through PHP if else statements and comparison operators, which is very simple~

Note:

When you write code in PHP, you often want to perform different actions for different decisions, then we can use conditional statements in the code to achieve this a little.

In PHP, the conditional statements we can use are as follows:

if statement: If the specified condition is true, the code will be executed;

if...else statement: If the condition is true, execute the code; if the condition is false, execute the other end of the code;

if...elseif....else statement: execute different code blocks based on two or more conditions;

switch statement: Select one of multiple code blocks to execute.

Finally, I would like to recommend the latest and most comprehensive "PHP Video Tutorial"~ Come and learn!

The above is the detailed content of Find maximum value from two positive integer values ​​within specified range via PHP. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
php
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template