Jump to content

[SOLVED] Help on a case statement trying to check first letter only


erikblue

Recommended Posts

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!

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

?> 

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?

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?

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.