Page 1 of 1

[SOLVED]convert swicth statement to if

Posted: Thu Oct 28, 2004 2:55 pm
by Kingo
Hello I'm trying to convert switch statement to if.
As i want to check for two conditions from the form.
Here is the switch code

Code: Select all

switch($_POST['action']){
	case "A":
		$to = $Aemail;
		$info = "";
		break;
	case "B":
		$to = $Bremail;
		$info = Binfo();
		break;
	case "C":
		$to = $Cemail;
		$info = Cinfo();	
		break;
	case "D":
		$to = $Demail;
		$info = Dinfo();
		break;
	case "E":
		$to = $Email;
		info = "";
		break;	
	
	default:
		exit("Unknown ");
}
Please help.

Posted: Thu Oct 28, 2004 3:08 pm
by Weirdan

Code: Select all

if($_POST['action'] == 'A') {
     $to = $Aemail;
     $info = "";
} elseif($_POST['action'] == 'B') {
     $to = $Bremail;
     $info = Binfo();
} elseif( .......
//and so on........
} else { 
    exit("Unknown ");
}
I prefer to use an array in such cases:

Code: Select all

$aInfo = arrray(
   'A' => array(
        'to' => 'an@email',
        'info' => '',
   ),
   'B' => array(
        'to' => 'another@email',
        'info' => 'BInfo',
   ),
//............
);
if(isset($_POST['action']) &&  isset($aInfo[$_POST['action']]) )  {
  $to = $aInfo[$_POST['action']]['to'];
  $info = $aInfo[$_POST['action']]['info'];
  if(is_callable($info)) 
     $info = call_user_func($info);
} else exit('Unknown');

Posted: Thu Oct 28, 2004 3:10 pm
by mudkicker

Code: Select all

<?php 
if($_POST['action'] == 'A')
{
// do A
}
elseif($_POST['action'] == 'B')
{
// do B
}
// ...
?>

Posted: Thu Oct 28, 2004 4:31 pm
by timvw
another one ;)

Code: Select all

if (isset($_POST['actio'n]))
{
      $action = $_POST['action'];

       switch($action)
        {
              case "A":
                  break;
        }
}

Posted: Fri Oct 29, 2004 7:31 am
by Kingo
Thanx very much.