Jump to content

switch statements


pommi

Recommended Posts

Hi Guys....  this will probably be an easy question.

 

Can you use swtich statements within an if statement...

 

So for example what i want to do it

 

if(!empty($aa))

  {

    switch($aa)

    { case: 1

      break;

      case 2:

      break;

    }

    else

    switch($bb)

    { case:1

      break;

      case: 2

      break;

    }

}

 

 

I have writen some code in this format and it isnt working??  Can anyone help...

 

Thanks ???

Link to comment
https://forums.phpfreaks.com/topic/133416-switch-statements/
Share on other sites

You're missing a slew of braces.  Proper indentation works wonder:

 

if (!empty($aa)) {
    switch ($aa) {
        case 1:
        break;

        case 2:
        break;
    }
} else {
    switch ($bb) {
        case 1:
        break;

        case 2:
        break;
    }
}

 

In reading your code, I also noticed a huge syntax error.  You need to put the : in the case statement AFTER the value.  case 1:, etc.

Link to comment
https://forums.phpfreaks.com/topic/133416-switch-statements/#findComment-693901
Share on other sites

example only.........

<?php


$aa="1";

if(empty($aa)) {
    
switch($aa){ 

	case "1" :
    
	break;
       
	case "2" :
       
	break;

}

}else{
    
    	switch($aa){ 

	case "1" :

       echo "hi redarrow";

	break;
       
	case "2" :
       
	break;

}	
}

?>

 

ur way...........

<?php


$aa="1";

if(!empty($aa)) {
    
switch($aa){ 

	case 1 :


              echo "hi redarrow";

	break;
       
	case 2 :
       
	break;

}

}else{
    
    	switch($aa){ 

	case 1 :

                break;
       
	case 2 :
       
	break;

}	
}

?>

Link to comment
https://forums.phpfreaks.com/topic/133416-switch-statements/#findComment-693907
Share on other sites

what u wanted sorry.........

 

<?php


$aa="1";

$bb="2";

if(!empty($aa)) {
    
   switch($aa){ 
      
      case 1 :


      	echo "hi redarrow first condition true";

      break;
       
      case 2 :
       
      break;
      
   }

}else{
    
       switch($bb){ 
      
      case 1 :
         
      break;
       
      case 2 :

             	echo "hi redarrow second condition true";

      break;
      
   }   
}

?>

Link to comment
https://forums.phpfreaks.com/topic/133416-switch-statements/#findComment-693913
Share on other sites

Thanks guys... to answer my question I assume I can add a switch within an If statement.

 

The problem though is within my coding...  Thanks!!

 

if/else/any other control statement just denote blocks that are only executed during certain conditions; they can contain any statements that the main program flow can.

Link to comment
https://forums.phpfreaks.com/topic/133416-switch-statements/#findComment-693928
Share on other sites

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.