0

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 applies 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";
} 
9
  • 4
    Dear me you like asking questions about nickels and dimes: stackoverflow.com/search?q=user:170622+dime Commented Dec 3, 2009 at 13:09
  • Lol indeed yeah ;) I've noticed too ;) +1 for you! Commented Dec 3, 2009 at 13:11
  • 1
    use if (isset($denomination["Nickels"]) not != NULL Commented Dec 3, 2009 at 13:16
  • 1
    I believe that the three sets of questions; (a) tagged "php", (b) asked by "Newb", and (c) related to a problem with denominations, are rapidly converging. Commented Dec 3, 2009 at 13:19
  • 1
    could you post the output from print_r($denomination) ? Commented Dec 3, 2009 at 13:21

4 Answers 4

2

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.

Sign up to request clarification or add additional context in comments.

Comments

2

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

Comments

0

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

1 Comment

No, but I assure you, I will be in the code cave with all this good stuff experimenting, until I get this baby of the ground, so yes I will by trying your code Chris Gutierrez, and thank you.
0

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.