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 November 1st, 2003, 12:58 PM
Registered User
Join Date: Nov 2003
Posts: 4
Thanks: 0
Thanked 0 Times in 0 Posts
Default Undefined variable

I am a widows user and when I feed a variable from a form and run the PHP file it gives this message :
Notice: Undefined variable: Number in d:\inetpub\wwwroot\dynamic.php on line 9

But when I tried the same thing in another web server, all things were Ok.
It seems to me that my local host is not configured properly.But I do not know what is missing.
Thank you for any idea.

Old November 2nd, 2003, 01:21 AM
Registered User
Join Date: Aug 2003
Posts: 7
Thanks: 0
Thanked 0 Times in 0 Posts
Send a message via MSN to cnilashis Send a message via Yahoo to cnilashis

Please give more specific information , like what version of PHP , what web server , your code , other wise it is not possible to tell what is wrong here . I am giving you two option for doing what you want to do :

(1) For PHP version 4.2.0 or above :
---------------> Script name : test.php <----------------

echo "The number is ----->" . $_POST['Number'];
---------------> Script Ends <---------------------------

---------------> Script name : form.php <----------------

echo "<form method=POST action=\"./test.php\">
<input type=text name=Number size=20> &nbsp;<input type=submit value=\"Submit\"></form>";
---------------> Script Ends <---------------------------

just run the script "form.php" , you should get the value.

(2) For PHP version 4.1.0 or less :

everything is same, just change the code of the file test.php to the following :

---------------> Script name : test.php <----------------

echo "The number is -----> $Number";
---------------> Script Ends <---------------------------

This change is necessary becuase the configuration settings register_globals which is turned off from PHP 4.2.0. So you must use the superglobal arrays to get a variable. please inform here if you still have problems.

nilashis chatterjee
Old November 2nd, 2003, 01:38 PM
Registered User
Join Date: Nov 2003
Posts: 4
Thanks: 0
Thanked 0 Times in 0 Posts

Thank you for your help.Your test script For PHP version 4.2.0 or above worked For I am using php-4.3.3 and the variables I used in the script was just $Number not $_POST['Number'];
But here I faced another problem : Whene I use a variable in such form like $Child[$Count] and changing the $Child with $_POST['Child'] it results in the following error:
Parse error: parse error, unexpected T_ENCAPSED_AND_WHITESPACE, expecting T_STRING or T_VARIABLE or T_NUM_STRING in d:\inetpub\wwwroot\dynamic2.php on line 10

As you see I use the examle from page 160 on BEGINNING PHP4 and they worked fine in the web server on my hosting account.
I use IIS server and thank you very much for you help.

Old November 3rd, 2003, 02:43 PM
Friend of Wrox
Join Date: Jun 2003
Posts: 836
Thanks: 0
Thanked 0 Times in 0 Posts

First, read up on the register_globals FAQ:

Your parse error is due to writing invalid PHP code. To access nested arrays, each array index is in it's own brackets:

  echo $_POST['Child'][$Count];

If you can't figure out your error, please post the PHP code that generates that error and we'll tell you what's wrong.

I'm going to take a guess, though: You're echoing your variables from within a string.

Read up on string types here: http://www.php.net/types.string

If your code looks like this:

echo "The value is $_POST['Child'][$Count]";

This won't work for a couple reasons.

First, when PHP parses an array in a double-quoted string, PHP assumes that the index part of that array will either be a string, a variable, or a number. Since the parser is already in a string, these tokens are parsed as strings, so a number isn't an integer, it's a number string. These values are extracted by the code parser. Each part is called a "token", and these tokens all have names, or identifiers, describing what kind of token it is. For example, the token name for a number string is T_NUM_STRING.

That all said, when PHP parses your line, the first character it reads in the array index is a single-quote mark. This confuses PHP since it's not a variable, number, or string. Well, technically it is a string, but PHP can't handle this as an automatically parsed index. The correct way to do this is to leave out the quotes:
  echo "The value is $_POST[Child]";

Second: Within a double quoted string, PHP can only parse up to a single level of indexing in an array. To do more, you'll need to exit the string and concatenate the value (1) or use curly-brace syntax (2). I prefer curly-brace syntax.

1: echo "The value is " . $_POST['Child'][$Count];
2: echo "The value is {$_POST['Child'][$Count]}";

Within curly-braces, PHP breaks out of regular string parsing mode and parses variables as if they were in regular context. Therefore, you do need to quote your string array indexes.

Again, I strongly suggest reading the relevant documentation:

Take care,

Old July 1st, 2005, 08:12 AM
Registered User
Join Date: Jul 2005
Posts: 1
Thanks: 0
Thanked 0 Times in 0 Posts

I'm having a problem with a new install of PHP 5.04 running with IIS6 on win 2003 server. My variables are not being passed in forms. For example I tried the form.php and tesp.php above. If I enter 1 in the textbox and submit I get
"Notice: Undefined index: Number in c:\Inetpub\wwwroot\admin\test.php on line 2
The number is ----->"
register-globals is off. Any ideas?

Similar Threads
Thread Thread Starter Forum Replies Last Post
Undefined variable adel_88 BOOK: Beginning PHP, Apache, MySQL Web Development ISBN: 978-0-7645-5744-6 2 July 9th, 2007 03:21 PM
Undefined Variable aspire Beginning PHP 4 July 12th, 2006 07:00 AM
undefined variable !! cody44 Beginning PHP 3 January 20th, 2005 05:47 PM
Undefined Variable fpolan Beginning PHP 9 July 15th, 2004 06:24 AM
Undefined Variable Some Help! knight Beginning PHP 7 February 19th, 2004 07:07 AM

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