Wrox Programmer Forums
Go Back   Wrox Programmer Forums > PHP/MySQL > Beginning PHP
|
Beginning PHP Beginning-level PHP discussions. More advanced coders should post to the Pro PHP forum.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the Beginning PHP 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 February 9th, 2005, 06:40 AM
Registered User
 
Join Date: Nov 2004
Posts: 5
Thanks: 0
Thanked 0 Times in 0 Posts
Send a message via MSN to Asix
Default Problem with defining variables

I've purchased a book "Beginning PHP4", and I've got a problem. In chapter 8, when it comes to switch pages using a $PHP_SELF global variable, I encounter a problem about undefined variable called $action. Here's the source code:

<?php
//selector.php
switch ($action)
{
   case "order_food":
      include "./waiter.php";
   break;
   case "play_hangman":
      include "./hangman.php";
   break;
   default:
      echo "Please choose what you'd like to do:<BR>";
      echo "<A HREF=$PHP_SELF?action=order_food>Order Food</A><BR>";
      echo "<A HREF=$PHP_SELF?action=play_hangman>Play Hangman</A>";
}
?>

When I run this on my local server, I get a following error:
"Notice: Undefined variable: action in c:\Inetpub\wwwroot\selector.php on line 3"

Is there any way to define the $action variable so I can switch pages?
 
Old February 9th, 2005, 09:38 AM
Authorized User
 
Join Date: Jan 2005
Posts: 20
Thanks: 0
Thanked 0 Times in 0 Posts
Default

$action has not yet been defined. Just because the default case is present, that doesnt mean that the switch can validate an uninitialized variable. At the beginning of your script I would add:

$action = "";
if (isset($_GET['action']))
     $action = $_GET['action']

That should fix that error. You may also want to turn register_globals on in your php.ini, although thats not a good thing to get in the habit of doing.

Jeff

 
Old February 9th, 2005, 10:22 AM
Registered User
 
Join Date: Nov 2004
Posts: 5
Thanks: 0
Thanked 0 Times in 0 Posts
Send a message via MSN to Asix
Default

Function register_globals is already turned on in my php.ini file. When I've added the code you posted, everything worked fine, but when I clicked on any choice, I get the following error code:

Parse error: syntax error, unexpected T_SWITCH in c:\Inetpub\wwwroot\selector.php on line 6

How can I avoid this?
 
Old February 9th, 2005, 10:43 AM
Authorized User
 
Join Date: Jan 2005
Posts: 20
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Sorry, I typed the code freehand without checking it. Add a semi-colon (;) after the
$action = $_GET['action'] line. Im surprised php ran initially without that.



 
Old February 9th, 2005, 11:27 AM
Authorized User
 
Join Date: Jan 2005
Posts: 82
Thanks: 0
Thanked 0 Times in 0 Posts
Default

I would remove the first line
$action = "" ; <---- will reset the value of action everytime page loads and we don't want that.


----------------
Never bother to learn something not knowing which does not do you any harm, and never neglect to learn something whose negligence will increase your ignorance - Imam Jafar Sadeq
 
Old February 13th, 2005, 10:49 AM
Registered User
 
Join Date: Nov 2004
Posts: 5
Thanks: 0
Thanked 0 Times in 0 Posts
Send a message via MSN to Asix
Default

Yeah, but that would return an error about undefined variable again, and that really is what we don't want. It's much easier to add $PHP_SELF global variable to every hyperlink in the script than to bother with this.

 
Old February 14th, 2005, 01:32 AM
Authorized User
 
Join Date: Jan 2005
Posts: 82
Thanks: 0
Thanked 0 Times in 0 Posts
Default

you can always add this line to the top of your pages:

error_reporting(0);


----------------
Never bother to learn something not knowing which does not do you any harm, and never neglect to learn something whose negligence will increase your ignorance - Imam Jafar Sadeq





Similar Threads
Thread Thread Starter Forum Replies Last Post
Defining a process snufse ASP.NET 2.0 Basics 5 April 23rd, 2008 07:13 AM
help with defining in the database derrida BOOK: Beginning PHP5, Apache, and MySQL Web Development ISBN: 978-0-7645-7966-0 0 April 14th, 2007 03:17 AM
Defining the Variables. dpkbahuguna Beginning VB 6 2 November 1st, 2006 12:40 PM
problem defining namespace for .aspx page pratik28 ASP.NET 2.0 Basics 0 October 26th, 2006 06:43 AM
Problem with defining cells in Excel VBA sunny76 Excel VBA 6 July 6th, 2005 04:59 AM





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