Wrox Programmer Forums
| Search | Today's Posts | Mark Forums Read
BOOK: Beginning PHP4/PHP 5 ISBN: 978-0-7645-4364-7; v5 ISBN: 978-0-7645-5783-5
This is the forum to discuss the Wrox book Beginning PHP4 by Wankyu Choi, Allan Kent, Chris Lea, Ganesh Prasad, Chris Ullman; ISBN: 9780764543647
Welcome to the p2p.wrox.com Forums.

You are currently viewing the BOOK: Beginning PHP4/PHP 5 ISBN: 978-0-7645-4364-7; v5 ISBN: 978-0-7645-5783-5 section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
 
Old November 7th, 2003, 11:15 AM
LL LL is offline
Registered User
 
Join Date: Nov 2003
Location: Montreal, Quebec, Canada.
Posts: 2
Thanks: 0
Thanked 0 Times in 0 Posts
Default querystrings in chapter 8

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!)
 
Old November 7th, 2003, 06:39 PM
Friend of Wrox
Points: 2,570, Level: 21
Points: 2,570, Level: 21 Points: 2,570, Level: 21 Points: 2,570, Level: 21
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Jun 2003
Location: San Diego, CA, USA
Posts: 836
Thanks: 0
Thanked 0 Times in 0 Posts
Default

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/
 
Old November 19th, 2003, 10:36 AM
LL LL is offline
Registered User
 
Join Date: Nov 2003
Location: Montreal, Quebec, Canada.
Posts: 2
Thanks: 0
Thanked 0 Times in 0 Posts
Default

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!





Similar Threads
Thread Thread Starter Forum Replies Last Post
Querystrings butchered abq23 Classic ASP Professional 2 April 7th, 2006 08:22 AM
Multiple Selects in SQL QueryStrings doonman BOOK: Beginning ASP.NET 2.0 BOOK VB ISBN: 978-0-7645-8850-1; C# ISBN: 978-0-470-04258-8 6 January 30th, 2006 07:01 AM
replacing querystrings with fake page names mase912 C# 0 November 24th, 2004 04:47 AM
Querystrings and Redirects hcweb Classic ASP Basics 3 July 30th, 2003 08:50 AM
Querystrings to Variables hcweb Classic ASP Basics 1 June 14th, 2003 06:54 PM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.