2017-06-16 45 views
0

我是新手,可能我的問題已經得到解答,但安迪哈里斯寫了一本書(請參閱書名)。我很喜歡這樣做,但是我有一個問題,我想在這裏發佈(我已經與他聯繫過,但沒有得到回覆)。有問題的代碼是:PHP 6 /絕對初學者的MySQL編程petals.php

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" 
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd"> 
<html lang="EN" dir="ltr" xmlns="http://www.w3.org/1999/xhtml"> 
<head> 
<title>petals.php</title> 
<link rel = "stylesheet" 
     type = "text/css" 
     href = "petals.css" /> 
</head> 
<body> 
<h1>Petals Around the Rose</h1> 

<?php 

printGreeting(); 
printDice(); 
printForm(); 

//$numPetals = filter_input(INPUT_POST, "numPetals"); 

function printGreeting(){ 
    global $numPetals; 
    $guess = filter_input(INPUT_POST, "guess"); 
    $numPetals = filter_input(INPUT_POST, "numPetals"); 

    if (!filter_has_var(INPUT_POST, "guess")){ 
    print "<h3>Welcome to Petals Around the Rose</h3>"; 
    } else if ($guess == $numPetals){ 
    print "<h3>You Got It!</h3>"; 
    } else { 

    print <<<HERE 

     <h3>from last try: </h3> 
     <p> 
     you guessed: $guess 
     </p> 
     <p> 
     -and the correct answer was: $numPetals petals around the rose 
     </p> 
HERE; 

    } // end if 

} // end printGreeting 

function showDie($value){ 
    print <<<HERE 
    <img src = "die$value.jpg" 
     height = "100" 
     width = "100" 
     alt = "die: $value" /> 

HERE; 
} // end showDie 

function printDice(){ 
    global $numPetals; 

    print "<h3>New Roll:</h3> \n"; 
    $numPetals = 0; 

    $die1 = rand(1,6); 
    $die2 = rand(1,6); 
    $die3 = rand(1,6); 
    $die4 = rand(1,6); 
    $die5 = rand(1,6); 

    print "<p> \n"; 
    showDie($die1); 
    showDie($die2); 
    showDie($die3); 
    showDie($die4); 
    showDie($die5); 
    print "</p> \n"; 

    calcNumPetals($die1); 
    calcNumPetals($die2); 
    calcNumPetals($die3); 
    calcNumPetals($die4); 
    calcNumPetals($die5); 

} // end printDice 


function calcNumPetals($value){ 
    global $numPetals; 

    switch ($value) { 
    case 3: 
     $numPetals += 2; 
     break; 
    case 5: 
     $numPetals += 4; 
     break; 
    } // end switch 

} // end calcNumPetals 

function printForm(){ 
    global $numPetals; 

    print <<<HERE 

    <h3>How many petals around the rose?</h3> 

    <form action = "" 
      method = "post"> 
    <fieldset> 
    <input type = "text" 
     name = "guess" 
     value = "0" /> 
    <input type = "hidden" 
     name = "numPetals" 
     value = "$numPetals" /> 
    <br /> 
    <input type = "submit" /> 
    </fieldset> 
    </form> 
    <p> 
    <a href = "petalHelp.html"> 
    give me a hint</a> 
    </p> 
HERE; 

} // end printForm 

?> 
</body> 
</html> 

他說,在他的書(對於那些誰擁有文本第95頁。):

這個函數[printGreeting()是指兩個$猜測, $ numPetals變量。由於其他函數都可能需要這兩個函數,因此它們在全局聲明中定義。請注意,您可以在單個全局命令中將 全局狀態分配給多個變量。

我看不到它們在全局聲明中的定義。可能超過它,但任何洞察力將不勝感激。

+0

global $ numPetals; –

+3

如果您在標題中使用帶有PHP 6的書,您可能需要一本新書。從未發生PHP 6 - 它可能會因此而大大過時。 – ceejayoz

+1

同上,書中給出了使用全局變量的例子。 –

回答

1

'statement'global將變量定義爲具有全局範圍,即使它在函數中,因此引用$ numPetals的任何內容都將訪問相同的值。

您可能稍後在教程中添加代碼,該代碼在函數之外使用$ numPetals變量,但這可能是書中「全局」概念的第一次介紹,這就是爲什麼要解釋這個概念的原因。

在PHP中,變量不一定必須在使用之前定義,這可能是造成混淆的原因。

+0

謝謝克里斯的解釋。這就說得通了。我想我只是努力與「全球$ numPetals」的第一個實例。花了我一段時間才意識到它在程序開始時可能會有價值。 – Jon