Home > Backend Development > PHP Tutorial > PHP Algorithm Exercise 11: Check whether two given integers are within the specified range

PHP Algorithm Exercise 11: Check whether two given integers are within the specified range

藏色散人
Release: 2023-04-10 15:22:01
Original
2968 people have browsed it

PHP algorithm series continues today~ Then in the previous article "PHP Algorithm Exercise 10: Calculating the Radius and Center Coordinates of a Circle" I introduced to you how to use PHP to calculate the radius and center coordinates of a circle. Interested friends can learn about it~

→Recommendation: "PHP algorithm practice series summary (continuously updated~)"

This article will introduce how to use it. PHP determines whether two given integers are within the specified range!

Specific questionThe description is "How to write a PHP program to check whether two given integers are within the range of 100..200 (inclusive)"?

I believe that everyone will be able to write this implementation method soon. The following is the method I gave, you can refer to it:

The PHP code is as follows:

<?php
function test($x, $y)
{
    return ($x >= 100 && $x <= 200) || ($y >= 100 && $y <= 200);
}

var_dump(test(100, 199));
var_dump(test(250, 300));
var_dump(test(105, 190));
Copy after login

The printed result is:

PHP Algorithm Exercise 11: Check whether two given integers are within the specified range

boolean true
boolean false
boolean true
Copy after login

It’s very simple! In the above code, we create a test method and give it two parameters, $x, $y;

The main thing you need to master here is the PHP operator, such as:

Comparison operator:

Greater than or equal to (x >= y): If x is greater than or equal to y, return true;

Less than or equal to (x < ;= y): If x is less than or equal to y, returns true;

Logical operator:

And (x && y): If x If both x and y are true, return true;

or (x || y): If at least one of x and y is true, return true.

Note: return returns program control to the calling module. Will continue with the next expression executed in the calling module.

If the return statement is called in a function, the execution of this function will be ended immediately and its parameters will be returned as the value of the function. return will also terminate the execution of the eval() statement or script file.

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

The above is the detailed content of PHP Algorithm Exercise 11: Check whether two given integers are within the specified range. 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