iStriide Posted August 14, 2011 Share Posted August 14, 2011 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. Quote Link to comment https://forums.phpfreaks.com/topic/244783-undefined-variable-error/ Share on other sites More sharing options...
PFMaBiSmAd Posted August 14, 2011 Share Posted August 14, 2011 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. Quote Link to comment https://forums.phpfreaks.com/topic/244783-undefined-variable-error/#findComment-1257328 Share on other sites More sharing options...
xyph Posted August 14, 2011 Share Posted August 14, 2011 $i++; is not defining a variable. Before calling $i++; you must first define $i using something like $i = 0; Quote Link to comment https://forums.phpfreaks.com/topic/244783-undefined-variable-error/#findComment-1257329 Share on other sites More sharing options...
iStriide Posted August 14, 2011 Author Share Posted August 14, 2011 Setting $i = 0; then doing $i++; isn't working. It's just putting 1 every time. Quote Link to comment https://forums.phpfreaks.com/topic/244783-undefined-variable-error/#findComment-1257332 Share on other sites More sharing options...
DavidAM Posted August 14, 2011 Share Posted August 14, 2011 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. Quote Link to comment https://forums.phpfreaks.com/topic/244783-undefined-variable-error/#findComment-1257336 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.