[SOLVED]convert swicth statement to if

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
Kingo
Forum Contributor
Posts: 146
Joined: Thu Jun 03, 2004 9:38 am

[SOLVED]convert swicth statement to if

Post 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.
Last edited by Kingo on Fri Oct 29, 2004 7:32 am, edited 1 time in total.
User avatar
Weirdan
Moderator
Posts: 5978
Joined: Mon Nov 03, 2003 6:13 pm
Location: Odessa, Ukraine

Post 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');
Last edited by Weirdan on Thu Oct 28, 2004 3:19 pm, edited 1 time in total.
User avatar
mudkicker
Forum Contributor
Posts: 479
Joined: Wed Jul 09, 2003 6:11 pm
Location: Istanbul, TR
Contact:

Post by mudkicker »

Code: Select all

<?php 
if($_POST['action'] == 'A')
{
// do A
}
elseif($_POST['action'] == 'B')
{
// do B
}
// ...
?>
timvw
DevNet Master
Posts: 4897
Joined: Mon Jan 19, 2004 11:11 pm
Location: Leuven, Belgium

Post by timvw »

another one ;)

Code: Select all

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

       switch($action)
        {
              case "A":
                  break;
        }
}
Kingo
Forum Contributor
Posts: 146
Joined: Thu Jun 03, 2004 9:38 am

Post by Kingo »

Thanx very much.
Post Reply