I'm getting post values from an emailer form and I set all the option values to numbers and I'm trying to convert them to their actual strings to send in an email. I need to make this code easier to use.
if ($subject == "1") {
$sub = "General Questions";
} elseif($subject == "2") {
$sub = "Membership";
} elseif($subject == "3") {
$sub = "Club Fees";
} elseif($subject == "4") {
$sub = "Proshop & Lessons";
} elseif($subject == "5") {
$sub = "Events";
} elseif($subject == "6") {
$sub == "Leagues & Programs";
} elseif($subject == "7") {
$sub == "Resturant & Bar";
};
Maybe I could set the value=''
to the actual values and skip this part all together.
Why not make an associative array?
$subs = array(
1 => 'General Questions',
2 => 'Membership',
3 => 'Club Fees',
4 => 'Proshop & Lessons',
// others.
);
$sub = isset($subs[$subject]) ? $subs[$subject] : 'Default Value';
One way to rewrite is to use the switch
statement in PHP. You can read more about it here .
Use a switch
statement, or map the potential outcomes to their respective values:
switch ( $subject ) {
case 1:
$sub = 'foo';
break;
case 2:
$sub = 'bar';
break;
}
Or
$values = array( "Foo", "bar" );
$sub = $values[ --$subject ];
Try this
<?php
switch ($subject)
{
case "1":
$sub = "General Questions";
break;
case "2":
$sub = "Membership";
break;
case "3":
$sub = "Club Fees";
break;
case "4":
$sub = "Proshop & Lessons";
break;
case "5":
$sub = "Events";
break;
case "6":
$sub = "Leagues & Programs";
break;
case "6":
$sub = "Resturant & Bar";
break;
default:
$sub = "";
}
?>
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.