random1 Posted October 18, 2010 Share Posted October 18, 2010 I've written the code to generate letter and number ranges: public function generateRange($type = 'numbers', $start = 0, $end = 10) { $rangelist = array(); switch($type) { case 'upper letters': { foreach(range($start, $end) as $letter) { $rangelist[] = $letter; } } case 'lower letters': { foreach(range($start, $end) as $letter) { $rangelist[] = $letter; } } case 'numbers': { foreach(range($start, $end) as $number) { $rangelist[] = $number; } } default: { return false; } return $rangelist; } } It current doesn't seem to return the array result correctly currently. If I change the line "$rangelist[] = $number;" to "echo $number;" it does print the correct data. Any ideas on why the array return line isn't working correctly? Quote Link to comment https://forums.phpfreaks.com/topic/216149-array-range-function/ Share on other sites More sharing options...
salathe Posted October 18, 2010 Share Posted October 18, 2010 Remember to break after each case. Quote Link to comment https://forums.phpfreaks.com/topic/216149-array-range-function/#findComment-1123342 Share on other sites More sharing options...
random1 Posted October 18, 2010 Author Share Posted October 18, 2010 I added in the break lines into my code: public function generateRange($type = 'numbers', $start = 0, $end = 10) { $rangelist = array(); switch($type) { case 'upper letters': { foreach(range($start, $end) as $letter) { $rangelist[] = $letter; } break; } case 'lower letters': { foreach(range($start, $end) as $letter) { $rangelist[] = $letter; } break; } case 'numbers': { foreach(range($start, $end) as $number) { $rangelist[] = $number; } break; } default: { return false; } return $rangelist; } } Seems to still not work. Any ideas? Quote Link to comment https://forums.phpfreaks.com/topic/216149-array-range-function/#findComment-1123344 Share on other sites More sharing options...
salathe Posted October 18, 2010 Share Posted October 18, 2010 The return line is in the wrong place. Also, those {}s for each case are not necessary. Quote Link to comment https://forums.phpfreaks.com/topic/216149-array-range-function/#findComment-1123347 Share on other sites More sharing options...
random1 Posted October 18, 2010 Author Share Posted October 18, 2010 Thanks I've ended up with: public function generateRange($type = 'numbers', $start = 1, $end = 10) { $rangelist = array(); switch($type) { case 'upper letters': { foreach(range($start, $end) as $letter) { $rangelist[] = $letter; } return $rangelist; break; } case 'lower letters': { foreach(range($start, $end) as $letter) { $rangelist[] = $letter; } return $rangelist; break; } case 'numbers': { foreach(range($start, $end) as $number) { $rangelist[] = $number; } return $rangelist; break; } default: { return false; } } } Quote Link to comment https://forums.phpfreaks.com/topic/216149-array-range-function/#findComment-1123353 Share on other sites More sharing options...
salathe Posted October 18, 2010 Share Posted October 18, 2010 Is there something more to your code that you are choosing to hide? I only ask because at the moment your function is doing nothing more than an ordinary call to range would do. Quote Link to comment https://forums.phpfreaks.com/topic/216149-array-range-function/#findComment-1123364 Share on other sites More sharing options...
Mchl Posted October 18, 2010 Share Posted October 18, 2010 While you're at it could you please write functions that: - turn all letters in a string to lowercase - find maximum number among any number of variables - sort array values - turn PHP array into JSON string I need these ASAP Quote Link to comment https://forums.phpfreaks.com/topic/216149-array-range-function/#findComment-1123367 Share on other sites More sharing options...
Pikachu2000 Posted October 18, 2010 Share Posted October 18, 2010 While you're at it could you please write functions that: - turn all letters in a string to lowercase - find maximum number among any number of variables - sort array values - turn PHP array into JSON string I need these ASAP LULZ. Quote Link to comment https://forums.phpfreaks.com/topic/216149-array-range-function/#findComment-1123426 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.