Home >Backend Development >PHP Tutorial >Use one line of PHP code to output all numbers between two integers that are divisible by 4
The topic of this article is "How to use one line of PHP code to output all numbers divisible by 4 between two integers". Be careful not to use any PHP control statements~
We all know that conditional control statements in PHP can help We implement many functions, but they are not allowed to be used in this article. However, since it is said to be done with one line of code, it means that it can be implemented very quickly and easily. If you already have an idea, then give it a try first!
→Here I will introduce to you the concept of "divisibility": If the integer b is divided by the non-zero integer a, the quotient is an integer, and the remainder is zero, we say that b is divisible by a (or Say a can divide b), b is the dividend, and a is the divisor, that is, a|b ("|" is the divisor symbol), which is read as "a divides b" or "b can be divided by a". a is called the divisor (or factor) of b, and b is called the multiple of a. Divisibility is a special case of division to exhaustion.
Let's follow the specific question: "Please write a PHP script to display all numbers between 200 and 250 that are divisible by 4."
to give the solution:
The PHP code is as follows:
<?php echo implode(",",range(200,250,4))."\n";
The result is as follows:
200,204,208,212,216,220,224,228,232,236,240,244,248
Isn’t it very simple, just one line of code!
The two key functions are:
range function, the range() function creates an array containing elements in the specified range; the function returns an array containing elements from low to high an array of elements.
→Note: If the low parameter is greater than the high parameter, the created array will be from high to low.
implode function, the implode() function is used to return a string composed of array elements.
→Note: The implode() function accepts two parameter orders. However, due to historical reasons, explode() does not work. You must ensure that the separator parameter comes before the string parameter.
Finally, I would like to recommend to you the latest and most comprehensive "PHP Video Tutorial"~ Come and learn!
The above is the detailed content of Use one line of PHP code to output all numbers between two integers that are divisible by 4. For more information, please follow other related articles on the PHP Chinese website!