php_b34st Posted August 8, 2008 Share Posted August 8, 2008 Is there a shorter way of doing this: if($position == '1') { $style = 'GK'; } if($position == '2') { $style = 'DEF'; } if($position == '4') { $style = 'MID'; } if($position == '6') { $style = 'STR'; } Link to comment https://forums.phpfreaks.com/topic/118813-clean-code/ Share on other sites More sharing options...
mbeals Posted August 8, 2008 Share Posted August 8, 2008 <?php switch($positoin){ case 1: $style = 'GK'; break; case 2: $style = 'DEF'; break; case 4: $style = 'MID'; break; case 6: $style = 'STR'; break; } ?> Link to comment https://forums.phpfreaks.com/topic/118813-clean-code/#findComment-611781 Share on other sites More sharing options...
proggR Posted August 8, 2008 Share Posted August 8, 2008 That was going to be my suggestion too Can you tell me though, with the case what if you have a multi line block of code to run in each case do you just put it in parenthesis with the break inside it? I can't remember and haven't done it in a while (but I'm going to switch most of my site over the the switch, case, default method of comparisons soon). Link to comment https://forums.phpfreaks.com/topic/118813-clean-code/#findComment-611790 Share on other sites More sharing options...
papaface Posted August 8, 2008 Share Posted August 8, 2008 That was going to be my suggestion too Can you tell me though, with the case what if you have a multi line block of code to run in each case do you just put it in parenthesis with the break inside it? I can't remember and haven't done it in a while (but I'm going to switch most of my site over the the switch, case, default method of comparisons soon). Would be: <?php switch($positoin){ case 1: $style = 'A lot of code'; echo 'A lot of code'; break; case 1: $style = 'A lot of code'; echo 'A lot of code'; break; case 1: $style = 'A lot of code'; echo 'A lot of code'; break; case 1: $style = 'A lot of code'; echo 'A lot of code'; break; } ?> No { } needed Link to comment https://forums.phpfreaks.com/topic/118813-clean-code/#findComment-611792 Share on other sites More sharing options...
proggR Posted August 8, 2008 Share Posted August 8, 2008 Okay thanks. I haven't used switch in years and couldn't remember. Link to comment https://forums.phpfreaks.com/topic/118813-clean-code/#findComment-611797 Share on other sites More sharing options...
php_b34st Posted August 8, 2008 Author Share Posted August 8, 2008 Thanks I will use that instead Link to comment https://forums.phpfreaks.com/topic/118813-clean-code/#findComment-611843 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.