Jump to content

Easy Php variable question


Evanthes

Recommended Posts

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!
Link to comment
Share on other sites

[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 work
GFD- 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
Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.