PHP 8.5.0 Alpha 1 available for testing

Voting

: max(two, seven)?
(Example: nine)

The Note You're Voting On

mohammed dot hussein dot mahmoud at gmail dot com
3 years ago
You could use negative numbers in place of the `step` parameter. You need to make sure that the `start` is bigger than `end`. Note that range() function in php generates the range inclusive, i.e. it also includes the `end` parameter and not just up to it but not including it like most other languages.
The following snippet of code should explain what I mean about negative steps:

<?php

// 100, 90, 80, 70, 60, 50, 40, 30, 20, 10, 0
print_r(range(100, 0, -10));

?>

What happens basically is that the range function does not really care about what is bigger or smaller, it just adds the step to the start and appends that to the a temp result variable as long as it did not reach the end param value. In this case, adding negative numbers is like minus (computers do that for 2's complement under the hood.) This will cause the number to go from 100 to 90 and then the function will check if 90 reached 0 yet. Since it wouldn't have done that, it will keep adding -step (-10 in that case) to the latest result (i.e. 90) and so on and so forth.

Since range() is said to be better and faster than array_fill() I believe it was important for me to try it out and actually post this note on the official documentation just to make sure people can use this.

<< Back to user notes page

To Top