代碼沒有錯誤,為什么得到的結(jié)果是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
舉報
2016-12-29
你把$sex的初始值換成"女" 程序輸出的$totalMoney=2300;因為第一個if ($sex == "男")的條件為false了所以就不執(zhí)行{$totalMoney = $basicMoney + 0;} 而else if($sex == "女")的條件為true 所以執(zhí)行{$totalMoney = $basicMoney + 300;} 最后輸出的$totalMoney=2300;
2017-01-02
echo寫到了php標簽的外面也能輸出o_O
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 程序就會執(zhí)行? 到? ? $totalMoney = $basicMoney + 0; 而不會執(zhí)行到else if ($sex == "女"){}里的語句,因為你給的變量$sex初始值為“男”所以 if($sex == "女") 為false? ,??? 在最后你用echo $totalMoney;因為$totalMoney=0;$basicMoney=2000;? 然后$totalMoney = $basicMoney + 0;運算的結(jié)果,所以$totalMoney=2000;