erikblue Posted November 16, 2007 Share Posted November 16, 2007 Ok, so i'm having trouble with a certain case in my switch statement that is checking a 3 digit character field($veh_cat). For one case in particular i only want the case to check only if the first letter is beginning with a 'B'. All of the other cases are defined for more specific 3 character matches. So I was trying to do a case using substr.... case "substr($veh_cat,0,1)='B'": But, that's not working. So i'm scratching my head here, I know this should be possible... but i'm not sure how to do it. Any help is fully appreciated! Quote Link to comment https://forums.phpfreaks.com/topic/77636-solved-help-on-a-case-statement-trying-to-check-first-letter-only/ Share on other sites More sharing options...
pocobueno1388 Posted November 16, 2007 Share Posted November 16, 2007 Change it to this case substr($veh_cat,0,1)=='B': EDIT: This code is tested, and works <?php $veh_cat = 'Abcde'; switch ($veh_cat){ case substr($veh_cat,0,1)=='A'; echo "Works"; break; default: echo "default"; } ?> Quote Link to comment https://forums.phpfreaks.com/topic/77636-solved-help-on-a-case-statement-trying-to-check-first-letter-only/#findComment-392969 Share on other sites More sharing options...
cooldude832 Posted November 16, 2007 Share Posted November 16, 2007 what you will need to do is nest this in the default case of your switch as I don't see a clear and easy way to have dynamic switch variables to test (you want to test 2 variables in theroy) so try <?php switch ($i) { case 0: echo "i equals 0"; break; case 1: echo "i equals 1"; break; case 2: echo "i equals 2"; break; } default: switch (substr($i,0,1){ case "a": echo "its a"; break; case "b": echo "its B"; break; } } ?> make sense? Quote Link to comment https://forums.phpfreaks.com/topic/77636-solved-help-on-a-case-statement-trying-to-check-first-letter-only/#findComment-392972 Share on other sites More sharing options...
erikblue Posted November 16, 2007 Author Share Posted November 16, 2007 Thanks for the fast responces! But, pocobueno's solution did not work. And as far as nesting it in the default case, I already have a default case which handles a variety of other 3-digit codes. So that wouldn't work for me then either would it? Quote Link to comment https://forums.phpfreaks.com/topic/77636-solved-help-on-a-case-statement-trying-to-check-first-letter-only/#findComment-392976 Share on other sites More sharing options...
pocobueno1388 Posted November 16, 2007 Share Posted November 16, 2007 Try my code again, I edited it. Quote Link to comment https://forums.phpfreaks.com/topic/77636-solved-help-on-a-case-statement-trying-to-check-first-letter-only/#findComment-392977 Share on other sites More sharing options...
erikblue Posted November 16, 2007 Author Share Posted November 16, 2007 Ah wonderful! I knew i was close! Thanks so much for you time pocobueno Quote Link to comment https://forums.phpfreaks.com/topic/77636-solved-help-on-a-case-statement-trying-to-check-first-letter-only/#findComment-392979 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.