Evanthes Posted January 4, 2007 Share Posted January 4, 2007 Hey guys I got a variable ($ncorr) that I'm using to count the numnber of correct answer for a test...everything works fine, except when i want to call the variable outside of the for statement check out my example[code=php:0]if ($ncorr == '10') {$mess = "you got ten right";echo "<font size='1' face='verdana'><center><h1>$ncorr/8!</h1><br/>";echo "$mess</center></font>";}echo "$ncorr answers<br>"; //variable shows up here}echo "$ncorr questions answered correctly"; //variable doesnt here[/code]Any help would be greatly appreciated!thanks! Quote Link to comment Share on other sites More sharing options...
kenrbnsn Posted January 4, 2007 Share Posted January 4, 2007 Can you post the rest of the code? The code snippet you posted doesn't have enough information for us to give a meaningful answer.Please post the code between [nobbc][code][/code][/nobbc] tags.Ken Quote Link to comment Share on other sites More sharing options...
fert Posted January 4, 2007 Share Posted January 4, 2007 change [code]echo "$ncorr questions answered correctly";[/code]to[code]echo "{$ncorr} questions answered correctly";[/code] Quote Link to comment Share on other sites More sharing options...
Eugene Posted January 4, 2007 Share Posted January 4, 2007 You got an extra bracket on line 5 or 7. when you want to divide. instead of $ncorr/8, use ".($ncorr/8)." Quote Link to comment Share on other sites More sharing options...
Evanthes Posted January 4, 2007 Author Share Posted January 4, 2007 [quote author=kenrbnsn link=topic=121036.msg497018#msg497018 date=1167943241]Can you post the rest of the code? The code snippet you posted doesn't have enough information for us to give a meaningful answer.Please post the code between [nobbc][code][/code][/nobbc] tags.Ken[/quote]The $ncorr variable is added via a if statement, then another if statement calculates the number of questions that were correct. the problem is that the variable doesnt hold its value outside of the if statements, as I want to use it in an email function. thanks a lot guys.:[code]function checkanswers($group1, $group2, $group3, $group4, $group5, $group6,$group7,$group8,$group9,$group10){$ncorr = '0';if ($group1 == "True") {echo "Answer to Question #1: $group1, You answered Correctly!<br>";$ncorr++;}else {echo "<Font color='red'>Question #1: The Correct Answer is: true, you answered: $group1<br></font>";}if ($group2 == "True") {echo "Answer to Question #2: $group2, You answered Correctly!<br>";$ncorr++;}else {echo "<Font color='red'>Question #2: The Correct Answer is: True, you answered: $group2<br></font>";}if ($group3 == "D") {echo "Answer to Question #3: $group3, You answered Correctly!<br>";$ncorr++;}else {echo "<Font color='red'>Question #3: The Correct Answer is: True, you answered: $group3<br></font>";}if ($group4 == "True") {echo "Answer to Question #4: $group4, You answered Correctly!<br>";$ncorr++;}else {echo "<Font color='red'>Question #4: The Correct Answer is: True, you answered: $group4<br></font>";}if ($group5 == "True") {echo "Answer to Question #5: $group5, You answered Correctly!<br>";$ncorr++;}else {echo "<Font color='red'>Question #5: The Correct Answer is: True, you answered: $group5<br></font>";}echo "You entered $group6 as an answer to Question 6<br>";echo "You entered $group7 as an answer to Question 7<br>";if ($group8 == "True") {echo "Answer to Question #8: $group8, You answered Correctly!<br>";$ncorr++;}else {echo "<Font color='red'>Question #8: The Correct Answer is: True, you answered: $group8<br></font>";}if ($group9 == "A") {echo "Answer to Question #9: $group9, You answered Correctly!<br>";$ncorr++;}else {echo "<Font color='red'>Question #9: The Correct Answer is: A, you answered: $group9<br></font>";}if ($group10 == "True") {echo "Answer to Question #10: $group10, You answered Correctly!<br>";$ncorr++;}else {echo "<Font color='red'>Question #10: The Correct Answer is: True, you answered: $group10<br></font>";}//------------------------------------------Results--------------------------------------------------if ($ncorr == '0') {$mess = "Some answers were incorrect, please backup and try again";echo "<font size='1' face='verdana'><center><h1>$ncorr/8!</h1><br/>";echo "$mess</center></font>";}if ($ncorr == '1') {$mess = "you got one right, please try again";echo "<font size='1' face='verdana'><center><h1>$ncorr/8!</h1><br/>";echo "$mess</center></font>";}if ($ncorr == '2') {$mess = "you got two right";echo "<font size='1' face='verdana'><center><h1>$ncorr/8!</h1><br/>";echo "$mess</center></font>";}if ($ncorr == '3') {$mess = "you got one right, please try again";echo "<font size='1' face='verdana'><center><h1>$ncorr/8!</h1><br/>";echo "$mess</center></font>";}if ($ncorr == '4') {$mess = "you got one right, please try again";echo "<font size='1' face='verdana'><center><h1>$ncorr/8!</h1><br/>";echo "$mess</center></font>";}if ($ncorr == '5') {$mess = "you got one right, please try again";echo "<font size='1' face='verdana'><center><h1>$ncorr/8!</h1><br/>";echo "$mess</center></font>";}if ($ncorr == '6') {$mess = "you got six right, please try again";echo "<font size='1' face='verdana'><center><h1>$ncorr/8!</h1><br/>";echo "$mess</center></font>";}if ($ncorr == '7') {$mess = "you got seven right, please try again";echo "<font size='1' face='verdana'><center><h1>$ncorr/8!</h1><br/>";echo "$mess</center></font>";}if ($ncorr == '8') {$mess = "you got eight right, two questions will be further graded";echo "<font size='1' face='verdana'><center><h1>$ncorr/8!</h1><br/>";echo "$mess</center></font>";}if ($ncorr == '9') {$mess = "you got nine right, please try again";echo "<font size='1' face='verdana'><center><h1>$ncorr/8!</h1><br/>";echo "$mess</center></font>";}if ($ncorr == '10') {$mess = "you got ten right";echo "<font size='1' face='verdana'><center><h1>$ncorr/8!</h1><br/>";echo "$mess</center></font>";}echo "$ncorr answers<br>"; //variable shows up here}echo "{$ncorr} questions answered correctly"; //variable doesnt here[/code]fert- didn't workGFD- I'm not trying to divide, just showing the user the number they got right out of the total number of questions.thanks for the help guys Quote Link to comment Share on other sites More sharing options...
kenrbnsn Posted January 4, 2007 Share Posted January 4, 2007 The structure of this code is horrible and impossible to follow.I notice that you are defining $ncorr in a function. If you are trying to use the value outside the function it won't work. This might be your problem.Ken Quote Link to comment Share on other sites More sharing options...
Evanthes Posted January 4, 2007 Author Share Posted January 4, 2007 hehe yeah I'm def not that great at coding...yeah I'll try defining it outside and see where that gets methanks Quote Link to comment Share on other sites More sharing options...
kenrbnsn Posted January 4, 2007 Share Posted January 4, 2007 If you define it outside the function and want to modify it in the function, you need to use the global statement within the function. A better solution would be to return the value.Ken Quote Link to comment Share on other sites More sharing options...
Evanthes Posted January 5, 2007 Author Share Posted January 5, 2007 how could i go about returning the value? Ii's working a bit better by defining it outside the function, my functions still work, and outside the functions I am getting 0 for a value instead of nothing like i had before. Quote Link to comment Share on other sites More sharing options...
emehrkay Posted January 5, 2007 Share Posted January 5, 2007 $val = function(); just make sure function() returns the val you want to return, then you can use $val all over the place 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.