php条件问题

时间:2009-12-03 13:05:43

标签: php

目标: 用户仅在给定时间为面额选择一个仅一个选项,其中某个值 , 一些 Dimes值 ,或者仅

问题:目前仅代码默认为仅限镍

预先存在的条件: 如果我选择不同的面额,例如仅限Quarters ,我只获得Nickels的值,这同样适用于Dimes?

代码段:

if($denomination["Nickels"] != NULL)

{
$value = $denomination["Nickels"];
echo $value . " is the value of selected Nickels";
}

else if ($denomination["Dimes"] != NULL)
{
$value = $denomination["Dimes"];
echo $value . " is the value of  selected Dimes";
}

else if ($denomination["Quarters"] != NULL)
{
$value = $denomination["Quarters"];
echo  $value . "is the value of selected Quarters";
} 

4 个答案:

答案 0 :(得分:2)

当第一个条件为真时,“elseif”停止处理(即“Nickels”)。 删除“else”并留下简单的“if”s。

答案 1 :(得分:2)

简而言之:

<?php
$value = array_shift(array_filter($denomination));
echo  $value . "is the value of selected Quarters";

使用此功能,您无需为每个新元素添加if。

答案 2 :(得分:0)

您是否明确将$ denomination数组中的值设置为null?

你有没有想过......

if($denomination["Nickels"])
{
$value = $denomination["Nickels"];
echo $value . " is the value of selected Nickels";
}

else if ($denomination["Dimes"])
{
$value = $denomination["Dimes"];
echo $value . " is the value of  selected Dimes";
}

else if ($denomination["Quarters"])
{
$value = $denomination["Quarters"];
echo  $value . "is the value of selected Quarters";
}

答案 3 :(得分:0)

尝试不使用elseifs,将其替换为ifs

if($denomination["Nickels"] != NULL)

{
$value = $denomination["Nickels"];
echo $value . " is the value of selected Nickels";
}

if ($denomination["Dimes"] != NULL)
{
$value = $denomination["Dimes"];
echo $value . " is the value of  selected Dimes";
}

if ($denomination["Quarters"] != NULL)
{
$value = $denomination["Quarters"];
echo  $value . "is the value of selected Quarters";
}