我是新手,可能我的問題已經得到解答,但安迪哈里斯寫了一本書(請參閱書名)。我很喜歡這樣做,但是我有一個問題,我想在這裏發佈(我已經與他聯繫過,但沒有得到回覆)。有問題的代碼是: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變量。由於其他函數都可能需要這兩個函數,因此它們在全局聲明中定義。請注意,您可以在單個全局命令中將 全局狀態分配給多個變量。
我看不到它們在全局聲明中的定義。可能超過它,但任何洞察力將不勝感激。
global $ numPetals; –
如果您在標題中使用帶有PHP 6的書,您可能需要一本新書。從未發生PHP 6 - 它可能會因此而大大過時。 – ceejayoz
同上,書中給出了使用全局變量的例子。 –