Php Error: Undefined Variable

Page 1 of 1

10 Replies - 8070 Views - Last Post: 01 December 2010 - 08:35 AM Rate Topic: -----

#1 congfuzi  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 4
  • Joined: 15-November 02

Php Error: Undefined Variable

Post icon  Posted 20 November 2002 - 10:38 PM

I tried to install couple of ready-to-use php programs, such as phpLinks and other CMS. The error messages would showed up with part of working page:"Undefined variable", even thought the "register_globals = off" in my php.ini file.

How could I fix it? :crazy:

Appreciate any helps.

Is This A Good Question/Topic? 0
  • +

Replies To: Php Error: Undefined Variable

#2 skyhawk133  Icon User is offline

  • Head DIC Head
  • member icon

Reputation: 1875
  • View blog
  • Posts: 20,282
  • Joined: 17-March 01

Re: Php Error: Undefined Variable

Posted 20 November 2002 - 10:40 PM

Probably means an included config file with other variables isn't loading properly... only reason something like that displays is when you have calls to variables that don't exist... usually if you look on the line that the error says to look on, you will find the culprit variable.
Was This Post Helpful? 0
  • +
  • -

#3 gneato  Icon User is offline

  • <title>Untitled Document</title>

Reputation: 0
  • View blog
  • Posts: 1,311
  • Joined: 03-September 01

Re: Php Error: Undefined Variable

Posted 20 November 2002 - 10:51 PM

congfuzi, on Nov 20 2002, 11:38 PM, said:

I tried to install couple of ready-to-use php programs, such as phpLinks and other CMS. The error messages would showed up with part of working page:"Undefined variable", even thought the "register_globals = off" in my php.ini file.

How could I fix it? :crazy:

Appreciate any helps.

Umm, that may be the problem...

try turning register_globals on
Was This Post Helpful? 0
  • +
  • -

#4 congfuzi  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 4
  • Joined: 15-November 02

Re: Php Error: Undefined Variable

Posted 20 November 2002 - 10:51 PM

variables is self defined in php, isn't?

made "register_globals = on", still doesn't work....
Was This Post Helpful? 0
  • +
  • -

#5 skyhawk133  Icon User is offline

  • Head DIC Head
  • member icon

Reputation: 1875
  • View blog
  • Posts: 20,282
  • Joined: 17-March 01

Re: Php Error: Undefined Variable

Posted 21 November 2002 - 01:14 AM

no, variables have to be defined... just not like

var $foo = "whatever";


but if you have like:
if ($foo != "") {....

then you will get an error cause the script is going "where the hell is foo?"
Was This Post Helpful? 0
  • +
  • -

#6 gneato  Icon User is offline

  • <title>Untitled Document</title>

Reputation: 0
  • View blog
  • Posts: 1,311
  • Joined: 03-September 01

Re: Php Error: Undefined Variable

Posted 21 November 2002 - 01:39 AM

If register_globals is on (please note that you *must* restart apache before that change takes effect, same for turning it off), variables used in the query string will be converted into regular variables in your php script; e.g.:

If you use this query string,

address.of/script.php?foo=bar

inside the script, $foo will equal "bar", or

echo $foo;

will output bar.

With register_globals off, you have to use $_GET["foo"].
Was This Post Helpful? 0
  • +
  • -

#7 FLYGOD  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 2
  • Joined: 04-March 02

Re: Php Error: Undefined Variable

Posted 21 November 2002 - 07:41 AM

However, if you are doing a script that has a possibility for checking variables that may not have been set (Say, someone went to the link, and a variable was not passed because that did not do it right, or they did not fill out something on a form) then you would get the error.

To correct that, you can use the 'isset' command to check if the variable is set first, and it will return true if it has content, false if it is empty. So, then you can have it skip the operation that uses the variable if it does not exist yet.

Also, as I make code that if it has a variable, it does one thing, if not it just fills in some default, I set my error messages to be off so there will be no errors and the script will work even though there would have been an error.
Was This Post Helpful? 0
  • +
  • -

#8 rpjd  Icon User is offline

  • D.I.C Head

Reputation: 1
  • View blog
  • Posts: 88
  • Joined: 08-March 07

Re: Php Error: Undefined Variable

Posted 21 March 2007 - 04:48 AM

View PostFLYGOD, on 21 Nov, 2002 - 07:41 AM, said:

However, if you are doing a script that has a possibility for checking variables that may not have been set (Say, someone went to the link, and a variable was not passed because that did not do it right, or they did not fill out something on a form) then you would get the error.

To correct that, you can use the 'isset' command to check if the variable is set first, and it will return true if it has content, false if it is empty. So, then you can have it skip the operation that uses the variable if it does not exist yet.

Also, as I make code that if it has a variable, it does one thing, if not it just fills in some default, I set my error messages to be off so there will be no errors and the script will work even though there would have been an error.


I am getting an undefined variable i error message

<?
int $i;
for($i=0; $i< pg_numrows($part_number_result); $i++)  //undefined variable i X3
	{																 //undefined variable i
	?>
	<td><?php echo $part_num_row[$i]; ?></td> //undefined variable i
	<?
	}
	?>

I have defined the $i as an int before my for loop but it still says i uis undefined. Am I right i saying variables do not have to be assigned a value upon declaration?

RPJD :)


[mod edit] Dangit rpjd, use the code tags. I'm getting tired of editing just about every one of your posts.
Was This Post Helpful? 0
  • +
  • -

#9 snoj  Icon User is offline

  • Married Life
  • member icon

Reputation: 84
  • View blog
  • Posts: 3,564
  • Joined: 31-March 03

Re: Php Error: Undefined Variable

Posted 21 March 2007 - 11:01 AM

FLYGOD, a variable can be set and empty and isset() will return true.

rpjd, I'm sure that doing int $i; is not correct php syntax. beside you don't need that since you're saying at the beginning of the for loop that $i = 0.
Was This Post Helpful? 0
  • +
  • -

#10 Guest_Erick Molnar*


Reputation:

Re: Php Error: Undefined Variable

Posted 01 December 2010 - 08:21 AM

View Postcongfuzi, on 20 November 2002 - 09:38 PM, said:

I tried to install couple of ready-to-use php programs, such as phpLinks and other CMS. The error messages would showed up with part of working page:"Undefined variable", even thought the "register_globals = off" in my php.ini file.

How could I fix it? :crazy:

Appreciate any helps.


On testing servers the default is usually to show all errors so it is common to get undefined variable errors if they are not predefined. When you upload your code to the server, they will in all likelihood disappear. But, since the goal is to write your code in a way that it will work under any conditions, the easiest way to get a handle on undefined variable errors is to encapsulate the offending variable in an isset function: isset($somevariable)

This method ensures that if the variable is not previously set, no errors will be generated, better practice than ignoring them.
Was This Post Helpful? 0

#11 CTphpnwb  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 2982
  • View blog
  • Posts: 10,299
  • Joined: 08-August 08

Re: Php Error: Undefined Variable

Posted 01 December 2010 - 08:35 AM

This topic is nearly 8 years old, so I doubt the OP is still interested.

Topic closed.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1