Jump to content

Undefined Variable Error


iStriide

Recommended Posts

I'm getting this error: "Notice: Undefined variable: i in C:\xampp\htdocs\halobattles\index.php on line 298". Not sure why I have this error, because I did define it as you can see here:

<?php

$i++;
		$start_battle_id = "start_battle$i";
		$start_battle = '"#start_battle'.$i.'"';
?>

 

All of this code is in a while loop. Not sure if that really effects anything.

Link to comment
https://forums.phpfreaks.com/topic/244783-undefined-variable-error/
Share on other sites

Where you defined it is not the same variable scope where you are using it.

 

In you need help determining why you are getting the error message, you will need to post all the code on the page that would be needed to duplicate the error.

You need to define $i outside of the loop. (show more code if you need help figuring out where)

 

Consider this:

 

$i++; is the same as $i = $i + 1;

 

In this case, the "$i =" is not evaluated until after the "$i + 1" is evaluated, and at that point, $i has not been defined

 

If you wrote:

 

$a = $b + 1;

 

Your error message is $b is undefined. Yes, "$a = ..." will define $a, but $b is not defined.

 

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.