5kyy8lu3 Posted January 19, 2009 Share Posted January 19, 2009 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 https://forums.phpfreaks.com/topic/141468-solved-switch-statement-unexpected-t_break/ Share on other sites More sharing options...
rhodesa Posted January 19, 2009 Share Posted January 19, 2009 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 https://forums.phpfreaks.com/topic/141468-solved-switch-statement-unexpected-t_break/#findComment-740494 Share on other sites More sharing options...
5kyy8lu3 Posted January 19, 2009 Author Share Posted January 19, 2009 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 https://forums.phpfreaks.com/topic/141468-solved-switch-statement-unexpected-t_break/#findComment-740498 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.