 |
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
|
|
|

February 9th, 2005, 06:40 AM
|
Registered User
|
|
Join Date: Nov 2004
Posts: 5
Thanks: 0
Thanked 0 Times in 0 Posts
|
|
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?
|

February 9th, 2005, 09:38 AM
|
Authorized User
|
|
Join Date: Jan 2005
Posts: 20
Thanks: 0
Thanked 0 Times in 0 Posts
|
|
$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
|

February 9th, 2005, 10:22 AM
|
Registered User
|
|
Join Date: Nov 2004
Posts: 5
Thanks: 0
Thanked 0 Times in 0 Posts
|
|
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?
|

February 9th, 2005, 10:43 AM
|
Authorized User
|
|
Join Date: Jan 2005
Posts: 20
Thanks: 0
Thanked 0 Times in 0 Posts
|
|
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.
|

February 9th, 2005, 11:27 AM
|
Authorized User
|
|
Join Date: Jan 2005
Posts: 82
Thanks: 0
Thanked 0 Times in 0 Posts
|
|
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
|

February 13th, 2005, 10:49 AM
|
Registered User
|
|
Join Date: Nov 2004
Posts: 5
Thanks: 0
Thanked 0 Times in 0 Posts
|
|
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.
|

February 14th, 2005, 01:32 AM
|
Authorized User
|
|
Join Date: Jan 2005
Posts: 82
Thanks: 0
Thanked 0 Times in 0 Posts
|
|
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
|
|
 |