Hey all, I’m a newbie here and probably my question has already been answered, but Andy Harris wrote a book (see post topic for name of book). I’ve enjoyed going through it, however I’ve got a question that I’d like to post here (I’ve reached out to him, but have not got a response). The code in question is:
<!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>
He says in his book (p. 95 for those who have the text):
“This function [printGreeting()] refers to both the $guess and $numPetals variables. Since both may be needed by other functions, they are defined in a global statement. Note that you can assign global status to more than one variable in a single global command.”
I don’t see where they are defined in a global statement. Probably overthinking it, but any insight would be much appreciated.
Kind regards,
Jon