Okay, this is basically what's happening:
$alphabet is an array where each element in the array is a different letter in the alphabet.
$letters is a string consisting of all the letters the user has guessed.
The links you're creating are how the user selects the next guess when playing the game.
The code creates a different link for each letter in the alphabet. Each link appends the letter you're guessing to the list of already-guessed letters.
You'll notice that the link query string is:
letters=$letters$var
So when you're making your first guess, $letters is empty. If you guess 'a', then the link you click will be "hangman.php?letters=A".
After clicking that link, $letters is the string, "A". Suppose you click the 'G' link. The href of that link will look like "hangman.php?letters=AG". See what's happening?
PHP substitutes the variable values in the string, transforming $letters$var into "AG".
Suppose you make a third guess, "X". $letters by this time is "AG", so the link you're clicking on is the link generated when $var is "X", so the complete link is "hangman.php?letters=AGX".
The reason that submitting form fields automatically modifies the global variable, $letters, is because the book was written when the register_globals configuration option was "on" by default. It's "off" now.
Read more about why "off" is a good thing:
http://p2p.wrox.com/archive/beginnin...2002-11/17.asp
Also, lots and lots and lots and lots of posts about this topic on the forums:
http://www.google.com/search?q=site:...ster%5Fglobals
The original code might be more readable if it was rewritten:
<?php
$alphabet = bla bla bla;
// initialize $guessed to '' if 'letters' var
// not submitted by link.
$guessed = isset($_GET['letters'])? $_GET['letters'] : '';
$self = $_SERVER['PHP_SELF'];
$links = '';
foreach ($alphabet as $letter)
{
$links .= "<a href=\"{$self}?letters={$guessed}{$letter}\">{$let ter}</a>\n";
}
echo $links;
?>
See, the variable names are slightly altered to be much more readable -- $var doesn't make as much sense as $letter. $link implies that the string only holds ONE link, but if you look at the loop, you don't ASSIGN to $link, you APPEND to it by concatenating the new string to the end of the existing string. That variable is renamed $links and explicitly initialized as an empty string before the loop.
Also: I use the curly-brace syntax for embedding variables in double-quoted strings, and I use the register_globals=off compliant variables: $_SERVER['PHP_SELF'] and $_GET['letters'].
Please let me know if it's still unclear!
Take care,
Nik
http://www.bigaction.org/