我有一个包含3个和4个字段的表名,我将使用这些表的if语句。目前,当只有两个参数时,以下代码运行良好,但如果它超过两个,我不知道如何使用它。
if ($history->getPsychological()) {
if($history->getPsychological() == 'Polymenore'){
$psychological = "Polymenore";
echo "<img height=\"30\" src=\"images/polymenorrhea.png\" width=\"250\" />\n";
} else {
$psychological = "Oligomenore";
echo "<img height=\"30\" src=\"images/oligomenorrhea.png\" width=\"250\" />\n";
}
}
在上面的代码中,如果心理是Polymenore,那么它将显示其图像,否则它将显示Oligomenore。
我的意思是,它的两个领域,1:Polymenore 2:Oligomenore,它运作良好。
所以,这是我的问题,
如果有两个以上的字段,我的意思是3和4个字段,如何在上面的代码中使用它:
例如:
如何将其与3个字段一起使用
心理与1:A 2:B 3:C
以及如何将其与4个字段一起使用
心理与1:A 2:B 3:C 4:D
提前致谢..
答案 0 :(得分:1)
<强> You're looking for elseif/else if
强>
语法:
if($foo){
//what happens if $foo is true
} elseif($bar){
//what happens if $foo is false and $bar is true
} elseif(...){
//any other checks ad infinitum
} else {
//what happens if no other case is true
}
你的例子:
if ($history->getPsychological()) {
if($history->getPsychological() == 'Polymenore'){
$psychological = "Polymenore";
echo "<img height=\"30\" src=\"images/polymenorrhea.png\" width=\"250\" />\n";
} elseif($history->getPsychological() == 'Oligomenore') {
$psychological = "Oligomenore";
echo "<img height=\"30\" src=\"images/oligomenorrhea.png\" width=\"250\" />\n";
} elseif(....){
//...
} else {
//...
}
}
答案 1 :(得分:1)
使用多个ifs:
if ($history->getPsychological()) {
if($history->getPsychological() == 'Polymenore'){
...
}
if($history->getPsychological() == 'other1')
...
}
if($history->getPsychological() == 'other2')
...
}
//$psychological = "Oligomenore";
echo "<img height=\"30\" src=\"images/oligomenorrhea.png\" width=\"250\"/>\n";
}
或切换:
switch (month) {
case 1: $history->getPsychological() == "'Polymenore'";
...
case 2: $history->getPsychological() == "other1";
...;
case 3: $history->getPsychological() == "March";
...;
default:...
答案 2 :(得分:1)
if ($history->getPsychological()) {
swtich( $history->getPsychological() )
{
case 'Polymenore':
{
$psychological = "Polymenore";
echo "<img height=\"30\" src=\"images/polymenorrhea.png\" width=\"250\" />\n";
break;
}
case 'Oligomenore':
{
$psychological = "Oligomenore";
echo "<img height=\"30\" src=\"images/oligomenorrhea.png\" width=\"250\" />\n";
break;
}
case '3':
{
....
break;
}
}
}