English 中文(简体)
在购买力平价中撰写大量其他说明的简单方式
原标题:A simple way to write large elseif statements in PHP
  • 时间:2012-04-29 04:25:18
  •  标签:
  • php
  • html

我从电子邮件形式获得职位价值,我把所有选择价值定在数字上,而Im试图将其转换到通过电子邮件发送的实际渠道。 我需要使这一守则更容易使用。

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";
};

也许我可以将<代码>价值=设定到实际价值中,并且把这一部分一并计算。

最佳回答

为什么不形成一个联系阵列?

$subs = array(
  1 =>  General Questions ,
  2 =>  Membership ,
  3 =>  Club Fees ,
  4 =>  Proshop &amp; Lessons ,
  // others.
);

$sub = isset($subs[$subject]) ? $subs[$subject] :  Default Value ;
问题回答

改写方式之一是使用PHP中的switch说明。 可在

使用<代码>switch说明,或将潜在结果与各自数值挂钩:

switch ( $subject ) {
  case 1:
    $sub =  foo ;
    break;
  case 2:
    $sub =  bar ;
    break;
}

$values = array( "Foo", "bar" );
$sub = $values[ --$subject ];

引言

<?php
switch ($subject)
{
case "1":
  $sub = "General Questions";
  break;
case "2":
  $sub = "Membership";
  break;
case "3":
  $sub = "Club Fees";
  break;
case "4":
  $sub = "Proshop &amp; Lessons";
  break;
case "5":
  $sub = "Events";
  break;
case "6":
  $sub = "Leagues &amp; Programs";
  break;
case "6":
  $sub = "Resturant &amp; Bar";
  break;
default:
  $sub = "";
}
?>




相关问题
CSS working only in Firefox

I am trying to create a search text-field like on the Apple website. The HTML looks like this: <div class="frm-search"> <div> <input class="btn" type="image" src="http://www....

image changed but appears the same in browser

I m writing a php script to crop an image. The script overwrites the old image with the new one, but when I reload the page (which is supposed to pickup the new image) I still see the old one. ...

Firefox background image horizontal centering oddity

I am building some basic HTML code for a CMS. One of the page-related options in the CMS is "background image" and "stretch page width / height to background image width / height." so that with large ...

Separator line in ASP.NET

I d like to add a simple separator line in an aspx web form. Does anyone know how? It sounds easy enough, but still I can t manage to find how to do it.. 10x!

热门标签