Jump to content

[SOLVED] Switch statement (unexpected T_BREAK)


5kyy8lu3

Recommended Posts

Hi.  I wrote a function that I send a single character string to that returns the long version of the cardinal direction.  If I send 'n' to the function, it returns 'north'.  Or it's supposed to anyhow.

 

I'm getting this error: Parse error: syntax error, unexpected T_BREAK on line 32

 

Here's the code:

function cardinal($string)
{
	switch ($string)
		{
			case "n":
				$ret = 'north';
				break;               //this is line 32 in my code
			case "e":
				$ret = 'east';
				break;
			case "s":
				$ret = 'south';
				break;
			case "w":
				$ret = 'west';
				break;
		}
	return($ret);
}

 

i've triple-checked the syntax on php.net, and i've searched the t_break parse error on google and I just can't seem to find out what I'm doing wrong.

 

if it helps any, i use the function up to 3 consecutive times if the direction is something like 'NNW' or 'SSE'.  depending on the string length i do different things.  if it's a length of 1 i just send it straight to the function.  if it has a length of 2 i put the first character into $a = substr($string, 0, 1) and the second into $b = substr($string, 1, 1); and send both to the function on the same like where i add the final product together like such:

 

		
$a = substr($wind_from, 0, 1);
$b = substr($wind_from, 1, 1);
$c = substr($wind_from, 2, 1);
$wind_from = ucfirst(cardinal($a)) . '-' . ucfirst(cardinal($b)) . cardinal($c);

 

thanks ahead of time.

 

Link to comment
Share on other sites

looks like you have some hidden characters in there. try this:

function cardinal($string)
   {
      switch ($string)
         {
            case "n":
               $ret = 'north';
               break;
            case "e":
               $ret = 'east';
               break;
            case "s":
               $ret = 'south';
               break;
            case "w":
               $ret = 'west';
               break;
         }
      return($ret);
   }

you can also shorten it by returning directly:

function cardinal($string)
   {
      switch ($string)
         {
            case "n": return 'north';
            case "e": return 'east';
            case "s": return 'south';
            case "w": return 'west';
         }
      return 'Unknown';
   }

Link to comment
Share on other sites

looks like you have some hidden characters in there. try this:

function cardinal($string)
   {
      switch ($string)
         {
            case "n":
               $ret = 'north';
               break;
            case "e":
               $ret = 'east';
               break;
            case "s":
               $ret = 'south';
               break;
            case "w":
               $ret = 'west';
               break;
         }
      return($ret);
   }

you can also shorten it by returning directly:

function cardinal($string)
   {
      switch ($string)
         {
            case "n": return 'north';
            case "e": return 'east';
            case "s": return 'south';
            case "w": return 'west';
         }
      return 'Unknown';
   }

 

sweet, I plugged in the 'shortened' code and it worked, I really need to set my default editor as notepad++, regular windows notepad keeps corrupting my code.  thanks for the help, i appreciate it.

Link to comment
Share on other sites

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.