Semas Posted December 20, 2008 Share Posted December 20, 2008 Hello everyone again. I'll try to explain what problems i have. 1st. When i define variables I'm keep getting: Notice: Undefined variable: i in D:\wamp\www\a.php on line 12 Notice: Undefined variable: i in D:\wamp\www\a.php on line 13 Notice: Undefined variable: i in D:\wamp\www\a.php on line 14 Notice: Undefined variable: i in D:\wamp\www\a.php on line 15 Notice: Undefined variable: i in D:\wamp\www\a.php on line 15 2Nd. Here is the code : <?php $i = 0; function rate($awsome, $very_good, $good, $not_good, $bad){ $total = $awsome + $very_good + $good + $not_good + $bad; $vawsome = $total / 100 * $awsome; $vvgood = $total / 100 * $very_good; $vgood = $total / 100 * $good; $vbad = $total / 100 * $not_good; $vvbad = $total / 100 * $bad; echo " <br />Awsome: "; while($i < round($vawsome)){ echo "|"; $i++; } $vawsome; echo " <br />Very good: "; while($i < round($vvgood)){ echo "|"; $i++; } $vvgood; echo " <br />Good: "; while($i < round($vgood)){ echo "|"; $i++; } $vgood; echo " <br />Bad: "; while($i < round($vbad)){ echo "|"; $i++; } $vbad; echo " <br />Very bad: "; while($i < round($vvbad)){ echo "|"; $i++; } $vvbad; } echo rate(1, 2, 3, 4, 5); ?> Apache2.2.11 || php5.2.8 Quote Link to comment Share on other sites More sharing options...
Mark Baker Posted December 20, 2008 Share Posted December 20, 2008 In the line while($i < round($vawsome)) what is the value of $i ?? Quote Link to comment Share on other sites More sharing options...
Semas Posted December 20, 2008 Author Share Posted December 20, 2008 $i = 0 One more thing: I edited the code: <?php $i = 0; function rate($awsome, $very_good, $good, $not_good, $bad){ $total = $awsome + $very_good + $good + $not_good + $bad; $vawsome = $awsome / $total * 100; $vvgood = $very_good / $total * 100; $vgood = $good / $total * 100; $vbad = $not_good / $total * 100; $vvbad = $bad / $total * 100; echo " <table border=\"0\">\n"; echo " <tr>\n <td align=\"right\">Awsome: </td>\n <td>"; while($i < round($vawsome)){ echo "|"; $i++; } echo $vawsome."</td>\n </tr>\n"; $i = 0; echo " <tr>\n <td align=\"right\">Very good: </td>\n <td>"; while($i < round($vvgood)){ echo "|"; $i++; } echo $vvgood."</td>\n </tr>\n"; $i = 0; echo " <tr>\n <td align=\"right\">Good: </td>\n <td>"; while($i < round($vgood)){ echo "|"; $i++; } echo $vgood."</td>\n </tr>\n"; $i = 0; echo " <tr>\n <td align=\"right\">Bad: </td>\n <td>"; while($i < round($vbad)){ echo "|"; $i++; } echo $vbad."</td>\n </tr>\n"; $i = 0; echo " <tr>\n <td align=\"right\">Very bad: </td>\n <td>"; while($i < round($vvbad)){ echo "|"; $i++; } echo $vvbad."</td>\n </tr>\n"; $i = 0; echo " </table>\n"; } ?> Quote Link to comment Share on other sites More sharing options...
Mark Baker Posted December 20, 2008 Share Posted December 20, 2008 $i = 0 Wrong. You're not setting $i to 0, so why do you believe it is 0? The first time you loop, $i has no value.... PHP creates it and gives it a value of 0, but also puts out a message to let you know that it has fixed an error in your code, namely it puts out a notice saying that $i isn't defined, which is exactly what you're seeing Quote Link to comment Share on other sites More sharing options...
Semas Posted December 20, 2008 Author Share Posted December 20, 2008 After i updated PHP to new version this is happening to every single variable in my all codes... Quote Link to comment Share on other sites More sharing options...
Mark Baker Posted December 20, 2008 Share Posted December 20, 2008 After i updated PHP to new version this is happening to every single variable in my all codes... Well it was almost certainly saying exactly the same in your previous version, but you probably had a different level of error logging in your php.ini file so that it suppressed notices. However, it's good practise to fix the errors, regardless of whether they are simply notices or not. Just set $i=0 before your first while loop Quote Link to comment Share on other sites More sharing options...
Eric_Ryk Posted December 21, 2008 Share Posted December 21, 2008 $i = 0 Wrong. You're not setting $i to 0, so why do you believe it is 0? The first time you loop, $i has no value.... PHP creates it and gives it a value of 0, but also puts out a message to let you know that it has fixed an error in your code, namely it puts out a notice saying that $i isn't defined, which is exactly what you're seeing Wrong. Semas is, just in the wrong place. Look at the top of the code. Anyways, you (Semas) need to take a look at variable scope and learn the difference between the global scope and the local scope. The initial declaration of $i is in the global scope, but it can't be accessed in the local function scope unless you explicitly declare it that way. However, since $i is a variable that should be encapsulated in the function, you should probably just move it into the function. Either way, look up variable scope on php.net. As far as it showing up with the new version, Mark Baker is correct. Quote Link to comment 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.