2009-11-21 58 views
0

我不知道如果我正確地做到這一點,用函數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."; 

而且是它甚至法律要做到這一點:

function getselection($selection,$price) 
{ 

具有原始功能defaultcoinload()內其他功能或多種功能,我認爲它是隻需要一點點澄清,謝謝,不要燃燒。

+0

你能提交完整的功能代碼嗎?似乎你錯過了一些代碼行... – 2009-11-21 02:26:48

+0

你的代碼對我來說沒有意義。也許你必須添加一些其他的代碼行。什麼是$ insertcoins? $ money是一個全局變量(php需要使用全局關鍵字來顯式擴展函數範圍)? – Eineki 2009-11-21 02:44:03

回答

1

看起來很好。這些陳述的順序可能令人困惑?大多數工程師會在調用函數聲明之前編寫函數聲明(你有它周圍的其他方法。)

兩件事情我看到的關注:

一個是任務分配時,它看起來像一個平等的比較更有意義:

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

另一種是$ money不是全球性的,因此在函數內分配它不會在其外面發生明顯的變化。通過在函數內部添加global $money來解決此問題。

總之,試試這個來代替:

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(); 

我已經刪除了很多東西,看起來像他們進行調試,但在此之後,你可以參考一下$錢。

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

我留給您識別和修復的其餘錯誤。

+0

謝謝,它仍然是一個測試版,有很多的錯誤,我需要專注於真正清理代碼,代碼已經從我發佈代碼到現在的時間發生了巨大變化,謝謝。 – Newb 2009-11-21 05:29:38

+0

函數聲明? function defaultcoinload(); 調用函數 defaultcoinload(){ } 一個 – Newb 2009-11-21 05:33:11

+0

功能實現的是 「函數func(){...無論以實現所需的邏輯...}」 函數聲明爲 「函數func();」注意分號。函數調用是「func();」 – wallyk 2009-11-22 07:46:15