Created
May 26, 2020 04:39
-
-
Save vuthaihoc/62c5d1dce4bbe27eb1ffd8af2ed8f430 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
/** | |
* $num can be 1 number, list or range | |
* Example | |
* $num = 1 => return [1] | |
* $num = "1,2,3,4" => return [1,2,3,4] | |
* $num = "1-3" => return [1,2,3] | |
*/ | |
function numbersGenerator($num) | |
{ | |
if (strpos($num, "-")) { | |
list($min, $max) = explode("-", $num); | |
for (; $min <= $max; $min++) { | |
yield (int)$min; | |
} | |
}else{ | |
$nums = explode(",", $num); | |
foreach($nums as $n){ | |
yield (int)$n; | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment