代码没有错误,为什么得到的结果是2000?
<?php
$totalMoney=0;
$basicMoney=2000;
$sex="男";
if ($sex == "男"){
$totalMoney = $basicMoney + 0;
}else if($sex == "女") {
$totalMoney = $basicMoney + 300;
}
?>
echo $totalMoney;
<?php
$totalMoney=0;
$basicMoney=2000;
$sex="男";
if ($sex == "男"){
$totalMoney = $basicMoney + 0;
}else if($sex == "女") {
$totalMoney = $basicMoney + 300;
}
?>
echo $totalMoney;
2016-12-29
<?php
$totalMoney=0;
$basicMoney=2000;
$sex="男";
if ($sex == "男"){
$totalMoney = $basicMoney + 0;
}else if($sex == "女") {
$totalMoney = $basicMoney + 300;
}
?>
echo $totalMoney;
从你的代码来看,你是用了if ..... else if 的控制流程语句,
因为你的变量 $totalMoney=0; 和 $basicMoney=2000; 都有了初始值, 而$sex的初始值是“男”
所以你判断性别等于男的话就是说 if ($sex == "男") 为true 程序就会执行 到 $totalMoney = $basicMoney + 0; 而不会执行到else if ($sex == "女"){}里的语句,因为你给的变量$sex初始值为“男”所以 if($sex == "女") 为false , 在最后你用echo $totalMoney;因为$totalMoney=0;$basicMoney=2000; 然后$totalMoney = $basicMoney + 0;运算的结果,所以$totalMoney=2000;
举报