是女性,没有房子,也怀孕,这个情况该如何加入这个程序中。
//多嵌套一个if循环
<?php
$totalMoney = 0;//总工资
$basicMoney = 300;//基本工资
$sex = "女";
$noHouse = FALSE; //是否有房
$houseMoney = 150;//住房补贴
$isPregnancy = TRUE; //是否怀孕
$pregnancyMoney = 100;//怀孕补贴
if($sex == "男")
{
$totalMoney = $basicMoney + 0;// 男的没奖金
if($noHouse){
$totalMoney = $totalMoney + $houseMoney;
if($isPregnancy){
$totalMoney = $totalMoney + 0; //男的没有怀孕补贴
}
}
}
else if($sex == "女")
{
$totalMoney = $basicMoney + 300;// 女的有奖金300元
if($isPregnancy){
$totalMoney = $totalMoney + $pregnancyMoney;//女的有怀孕补贴
if($noHouse){
$totalMoney = $totalMoney + $houseMoney;
}
}
}
echo $totalMoney;
?>
<?php
$totalMoney = 0;//总工资
$basicMoney = 300;//基本工资
$sex = "女";
$noHouse = TRUE; //是否有房
$houseMoney = 150;//住房补贴
$isPregnancy = TRUE; //是否怀孕
$pregnancyMoney = 100;//怀孕补贴
if($sex=="男")
{
$totalMoney = $basicMoney + 0;// 男的没奖金
if($noHouse)
{
$totalMoney = $totalMoney + $houseMoney;
}
}
else if($sex=="女")
{
$totalMoney = $basicMoney + 300;// 女的有奖金300元
if($isPregnancy)
{
$totalMoney = $totalMoney + $pregnancyMoney; //女的怀孕,则发放怀孕补贴
}
if($noHouse)
{
$totalMoney = $totalMoney + $houseMoney; //女的没房子,则发放住房补贴
}
}
echo $totalMoney;
?>
这是比较直观的写法。
在写一个if