Jump to content

Recommended Posts

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?

Link to comment
https://forums.phpfreaks.com/topic/216149-array-range-function/
Share on other sites

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?

Thanks :D

 

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;
		}
	}
}

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.

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.