我只是学习PHP代码而且我在下面有一个IF语句似乎不起作用。我有其他IF语句,如下面但是有3个或更少的OR语句,它们似乎只能找到。
if ($AT_Med_1_3 == '' or $AT_Med_1_3 == NULL or $AT_Med_1_3 == ' ' or $AT_Med_1_3 = 'None')
{`enter code here`
// Echo 'Doing $AT_ Input';
?>
<select name="AT_Med_1_3">
<option value="None">None</option>
<option value="Oral_Zofran">Oral Zofran</option>
<option value="IV_Zofran">IV_Zofran</option>
<option value="Oxygen">Oxygen</option>
<option value="Ibuprofen">Ibuprofen</option>
<option value="Aetaminothen">Aetaminothen</option>
<option value="Upinephrne">Upinephrne</option>
<option value="Xopenex">Xopenex</option>
<option value="Albuterol">Albuterol</option>
<option value="Valium">Valium</option>
<option value="Magnesiunsulphate">Magnesiunsulphate</option>
<option value="Diphenhyamine">Diphenhyamine</option>
<option value="Ketorolac">Ketorolac</option>
<option value="Promethazion">Promethazion</option>
<option value="Oral_Fluids_Soup">Oral Fluids Soup</option>
<option value="Oral_Fluids_Electrolyte">Oral Fluids Electrolyte</option>
<option value="Oral_Fluids_Water">Oral Fluids Water</option>
</select>
<?php
} else {
// echo 'Showing $AT_';
echo $AT_Med_1_3;
echo nl2br("\n");
}
?>
如果我将IF语句更改为几个If else语句,它可以工作但是代码很难看
<?php
// if ($AT_Med_1_1 == '' or $AT_Med_1_1 == NULL or $AT_Med_1_1 == ' ' or $AT_Med_1_1 = 'None')
if ($AT_Med_1_1 == '')
{
goto test1;
} else if ($AT_Med_1_1 == NULL)
{
goto test1;
} else if ($AT_Med_1_1 == ' ')
{
goto test1;
} else if ($AT_Med_1_1 == 'None')
{
goto test1;
}
goto test;
// Echo 'Doing Input';
test1:
?>
<select name="AT_Med_1_1">
<option value="None">None</option>
<option value="Oral_Zofran">Oral Zofran</option>
<option value="IV_Zofran">IV_Zofran</option>
<option value="Oxygen">Oxygen</option>
<option value="Ibuprofen">Ibuprofen</option>
<option value="Aetaminothen">Aetaminothen</option>
<option value="Upinephrne">Upinephrne</option>
<option value="Xopenex">Xopenex</option>
<option value="Albuterol">Albuterol</option>
<option value="Valium">Valium</option>
<option value="Magnesiunsulphate">Magnesiunsulphate</option>
<option value="Diphenhyamine">Diphenhyamine</option>
<option value="Ketorolac">Ketorolac</option>
<option value="Promethazion">Promethazion</option>
<option value="Oral_Fluids_Soup">Oral Fluids Soup</option>
<option value="Oral_Fluids_Electrolyte">Oral Fluids Electrolyte</option>
<option value="Oral_Fluids_Water">Oral Fluids Water</option>
</select>
<?php
goto endtest;
// } else {
// echo 'Showing $AT_';
test:
echo $AT_Med_1_1;
echo nl2br("\n");
endtest:
// }
?>
我愿意更好地建议如何编写这个IF语句。
答案 0 :(得分:2)
我认为您的主要问题是=
而非==
,但为了简化if
,trim()
会将任何空格删除到''
和{ {1}}检查empty()
和''
:
PHP&lt; 5.5.0 强>
null
PHP&gt; = 5.5.0
$AT_Med_1_1 = trim($AT_Med_1_1);
if (empty($AT_Med_1_1) || $AT_Med_1_1 == 'None')
请注意,if (empty(trim($AT_Med_1_1)) || $AT_Med_1_1 == 'None')
还会将empty()
和false
值(0
和"0"
)视为空。
在您的情况下,大多数情况下,0.0
也是正确的OR运算符。