开发者

php conditional problem

开发者 https://www.devze.com 2022-12-13 15:42 出处:网络
Goal: User only selects one, only one option at a given time for the denomination, either some value of Nickels only,

Goal: User only selects one, only one option at a given time for the denomination, either some value of Nickels only, some value of Dimes only, or some value of Quarters only.

Problems: Currently the code only defaults to Nickels only?

Pre-existing condtion: If I select a different denomination, for example, Quarters only, I only get the values for Nickels, the same ap开发者_高级运维plies for Dimes?

Code Snippet:

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";
} 


To make it short:

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

With this you don't have to put in a if for every new element.


The "elseif" stops processing when the first condition is true (i.e. "Nickels"). Remove the "else"s and leave plain "if"s.


Are you explicitly setting the values in the $denomination array to null?

Have you trying something like...

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";
}


Try not using the elseifs, replace them with 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";
}
0

精彩评论

暂无评论...
验证码 换一张
取 消