PHP 6/MySQL Programming for the Absolute Beginner petals.php question

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

Here is one place:

function printGreeting(){
  global $numPetals;

In PHP, globals are defined inside the function that wishes to get access to global variables, rather than in the main code.

ETA - I don’t see anywhere he uses $guess as a global variable, only $numPetals, but the guess is only used in one function so probably not needed.

EATA - I agree with @dormilich below, steer clear of globals if you can.

1 Like

Best you forget that you can define global variables. They are nothing but trouble, esp. when writing tests.

if you need a variable in a function, pass it as a parameter!

2 Likes

Does a title have a typo? I ask because PHP 6 was never released and will never be. If the the book’s title was actually PHP 6 then I would toss it. If you meant 7 then fine.

3 Likes

before the decision was made to skip the version number six, a couple of books were already out assuming PHP 6 would be next.

1 Like

And I would argue that any book that has not been updated to PHP 7 should not be used. Especially by beginners. Just look at the example. xhtml!!! Really?

On the other hand, kudos for using the heredoc syntax. You don’t see much of the that in beginner books.

1 Like

Ok, thank you for your good explanation. Your insight regarding the scope of $guess makes sense and I’m slowly beginning to wrap my mind around the purpose of global. As I see it, the declaration of global $numPetals simply says “grab whatever the value of $numPetals is at this point in the program flow.”

It’s not a typo - it just came out ahead of the game - anticipating PHP6.

No, that’s not correct, it’s not grabbing a copy of the value of the variable at that point, it’s grabbing a pointer to the global variable so that it can access it directly. That is, when you assign a value to a global variable inside a function, the value of that variable outside the function will change as well. This facility is used in the code you posted above, where one function takes the value from an input variable, another does some calculations on it.

Your analogy would be correct if you were passing $numPetals into the function as a parameter (using the default “call by value”) - then you’d get the value of the variable when the function is called, but anything you do inside the function would not change the value of the variable outside it.

1 Like

This topic was automatically closed 91 days after the last reply. New replies are no longer allowed.