Hi, i was wondering if somebody can help me to create a function to generate values with the following output:
0, 1, 2, 4, 8, 16, 32,... up until some number .
This may be a dumb question, but i don't know how to do this.
Thanks in advance.
generate an array of these numbers?It's fairly simple/short. So see below, if you have any questions be sure to ask!
function numberGenerator($limit){ $counter = 0; $array = array(); while($counter <= $limit){ $array[] = $counter; if($counter > 0) $counter *= 2; else $counter = 1; } return $array; } print_r(numberGenerator(32));
I think the example could be simplified to:
function numberGenerator($limit) { $array = array(); if ($limit >= 0) { $array[] = 0; for ($counter = 1; $counter <= $limit; $counter += $counter) { $array[] = $counter; } } return $array; }
Thank you so much to everyone.
Problem solved.
You're welcome! Kindly mark this thread as resolved using the thread tools.