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
Kingo
Forum Contributor
Posts: 146 Joined: Thu Jun 03, 2004 9:38 am
Post
by Kingo » Thu Oct 28, 2004 2:55 pm
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.
Weirdan
Moderator
Posts: 5978 Joined: Mon Nov 03, 2003 6:13 pm
Location: Odessa, Ukraine
Post
by Weirdan » Thu Oct 28, 2004 3:08 pm
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.
mudkicker
Forum Contributor
Posts: 479 Joined: Wed Jul 09, 2003 6:11 pm
Location: Istanbul, TR
Contact:
Post
by mudkicker » Thu Oct 28, 2004 3:10 pm
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 » Thu Oct 28, 2004 4:31 pm
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 » Fri Oct 29, 2004 7:31 am
Thanx very much.