|
Subject:
|
querystrings in chapter 8
|
|
Posted By:
|
LL
|
Post Date:
|
11/7/2003 10:15:22 AM
|
Hi!
In the "hangman" exercise in ch. 8, I don't understand how the chosen letters accumulate in the "$letters" variable in the querystring. In short, I don't understant why this works:
<?php $alphabet=array("A","B","C","D","E","F","G","H","I","J","K","L","M","N","O","P","Q","R","S","T","U","V","W","X","Y","Z");
foreach ($alphabet as $var) { $link .= "<a href=\"$PHP_SELF?letters=$letters$var\">$var</a> "; } echo $link; ?>
Can you help my confused mind? 
Thanks LL (By the way, I LOVE this book. It's good not only for PHP, but also for understanding the basics of web programming. If all Wrox books are like this, you have a new fan!)
|
|
Reply By:
|
nikolai
|
Reply Date:
|
11/7/2003 5:39:36 PM
|
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/beginning_php/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:p2p%2Ewrox%2Ecom+register%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}\">{$letter}</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/
|
|
Reply By:
|
LL
|
Reply Date:
|
11/19/2003 9:36:07 AM
|
Thanks for the detailed explanation. I understand better now. I had read the other posts in the forum concerning this issue, but I guess that what threw me off was everybody saying that register_globals was now "off" by default. Since the script worked on my hosting server, I thought it couldn't be from using a global variable. But I guess that means that the host I'm with still has it turned "on" by default, which I'll look into.
But anyway, your answer helped me understand the script much better. Thanks again for clearing that up for me!
|
|