0

I am not sure if I am doing this correctly,pre-loading my array values for coins with the function defaultcoindload();

defaultcoindload();
function defaultcoindload()
{
/*Have the coin loads up to a maximum of a 1.00 dollar in value:
An associative array-ID key is associated with  value(ID is Nickels with value of 20).
$money = array("Nickels"=>20, "Dimes"=>10, "Quarters"=>10);
The array code for $money above  is  the same as the array code below,
with the difference being the structure and the ID keys can be accessed in a script*/

if ($money < 1.00)
{
echo "money";
}
else if($money = $insertcoins[$selection])
{
echo "$selection";
}


 $money['Nickels'] = "20";
 $money['Dimes'] = "10";
 $money['Quarters'] = "10";
 echo "The value of Nickels is " . $money['Nickels'] ." cents.";

Furthermore is it even legal to do this:

function getselection($selection,$price)
{

Have another function or multiple functions inside of the original function defaultcoinload(), I think it is just need a little clarification, thank you, for not flaming.

2
  • Can you submit complete function code? Seems you missed some code lines... Commented Nov 21, 2009 at 2:26
  • Your code doesn't make sense to me. Maybe you have to add some other lines of code. What is $insertcoins? $money is a global variable (php need an explicit extension of a function scope, with the global keyword)? Commented Nov 21, 2009 at 2:44

1 Answer 1

1

It looks fine. Perhaps the order of the statements is confusing? Most engineers would write the function declaration before calling it. (You have it the other way around.)

Two things I see of concern:

One is an assignment where it looks like an equality comparison makes more sense:

if ($money == $insertcoins[$selection])

The other is that $money isn't global, so assigning it within the function won't make a visible change outside it. Fix this by adding global $money inside the function.

In summary, try this instead:

function defaultcoindload()
{
    /* Have the coin loads up to a maximum of a 1.00 dollar in value:
     * An associative array-ID key is associated with  value (ID is Nickels with value of 20).
     * $money = array("Nickels"=>20, "Dimes"=>10, "Quarters"=>10);
     * The array code for $money above is the same as the array code below,
     * with the difference being the structure and the ID keys can be accessed in a script
     */
    global $money;

    $money['Nickels'] = 20;
    $money['Dimes']   = 10;
    $money['Quarters'] = 4;
    echo "The value of Nickels is " . $money['Nickels'] ." cents.";
}

defaultcoindload();

I've removed a lot of things which look like they were for debugging, but after this, you can reference $money.

print_r($money);  // show all the money

The remaining bugs I leave for you to identify and fix.

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

3 Comments

Thanks, it's still a Beta, there are a lot of bugs, I need to focus on really cleaning up the code, the code has dramatically changed from the time that I posted the code to now, thank you.
Function declaration? function defaultcoinload(); Call the function defaultcoinload() { }
A function implementation is "function func() { ... whatever logic needed to implement ... }" A function declaration is "function func ();" Note the semicolon. A function call is "func();"

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.