note 126749 added to function.range

From: Date: Tue, 04 Jan 2022 18:47:01 +0000
Subject: note 126749 added to function.range
Groups: php.notes 
Request: Send a blank email to php-notes+get-217471@lists.php.net to get a copy of this message
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. ---- Server IP: 45.112.84.4 Probable Submitter: 80.90.5.135 (proxied: 156.194.103.241) ---- Manual Page -- https://php.net/manual/en/function.range.php Edit -- https://main.php.net/note/edit/126749 Del: integrated -- https://main.php.net/note/delete/126749/integrated Del: useless -- https://main.php.net/note/delete/126749/useless Del: bad code -- https://main.php.net/note/delete/126749/bad+code Del: spam -- https://main.php.net/note/delete/126749/spam Del: non-english -- https://main.php.net/note/delete/126749/non-english Del: in docs -- https://main.php.net/note/delete/126749/in+docs Del: other reasons-- https://main.php.net/note/delete/126749 Reject -- https://main.php.net/note/reject/126749 Search -- https://main.php.net/manage/user-notes.php

« previous php.notes (#217471) next »