PHP Aritmetic Operators
<?php
$a == \"1\";
$a + $a == \"$total\";
print (\"$total\");
?>
I'm just wondering why the above code wont work? I'm sure there is some thing I'm missing here..
<?php
$a == \"1\";
$a + $a == \"$total\";
print (\"$total\");
?>
I'm just wondering why the above code wont work? I'm sure there is some thing I'm missing here..
Mark Hensler posted this at 08:02 — 20th November 2002.
He has: 4,048 posts
Joined: Aug 2000
$a == "1";
== is a comparison opperator, not an assignment opperator
$a + $a == "$total";
Even if we assume this was corrected to:
$a + $a = "$total";
This still would not work because $a + $a is an expression and cannot be assigned a value.
Mark Hensler
If there is no answer on Google, then there is no question.
nuk3 posted this at 08:17 — 20th November 2002.
They have: 238 posts
Joined: May 2002
I'm creating a quiz script and I need to add up all the values that were correct. I'm wondering how I would go about this.
Here is the code, I know its bulky but I'm still learning..
<?php
$right = \"You got it right!\";
$wrong = \"You got it wrong!\";
$question = array ( \"This Quiz is made by who?\", \"Question2\", \"Question3\", \"Question4\", \"Question5\", \"Question6\", \"Question7\", \"Question8\", \"Question9\", \"Question10\" );
$answer = array ( \"Owen\", \"Blah2\", \"Blah3\", \"Blah4\", \"Blah5\", \"Blah6\", \"Blah7\", \"Blah8\", \"Blah9\", \"Blah10\" );
print (\"Question 1 :\");
print (\"Question: $question[0]\");
print (\"Your Answer: $q1\");
print (\"Correct Answer: $answer[0]\");
print (\"Result: \");
if ($answer[0] == \"$q1\") {
print (\"$right\"); } else {
print (\"$wrong\");}
print (\"</font>\");
?>
The $q1 variable is delivered from the option that the user selects on the previous page. I so far only have 1 question implemented. For an online demo go HERE.
Mark Hensler posted this at 08:27 — 20th November 2002.
He has: 4,048 posts
Joined: Aug 2000
try this:
<?php
$cnt_right = 0;
$cnt_wrong = 0;
$question = array ( \"This Quiz is made by who?\", \"Question2\", \"Question3\", \"Question4\", \"Question5\" );
$answer = array ( \"Owen\", \"Blah2\", \"Blah3\", \"Blah4\", \"Blah5\" );
$i=0;
while (isset($question[$i]) && isset($answer[$i])) {
print (\"Question \".($i+1).\": $question[$i]\n\");
print (\"Your Answer: \".${'q'.$i}.\"\n\");
print (\"Correct Answer: $answer[$i]\n\");
print (\"Result: \");
if ($answer[$i] == ${'q'.$i}) {
print (\"You got it right!\");
$cnt_right++;
}
else {
print (\"You got it wrong!\");
$cnt_wrong++;
}
$i++;
}
print (\"Final Results:\n\");
print (\"Total Correct: $cnt_right\n\");
print (\"Total Wrong: $cnt_wrong\n\");
?>
Mark Hensler
If there is no answer on Google, then there is no question.
nuk3 posted this at 08:30 — 20th November 2002.
They have: 238 posts
Joined: May 2002
I dont understand a thing above lol
Mark Hensler posted this at 08:39 — 20th November 2002.
He has: 4,048 posts
Joined: Aug 2000
a few comments inset...
<?php
// counters
$cnt_right = 0;
$cnt_wrong = 0;
// array of questions and correct answers
$question = array ( \"This Quiz is made by who?\", \"Question2\", \"Question3\", \"Question4\", \"Question5\" );
$answer = array ( \"Owen\", \"Blah2\", \"Blah3\", \"Blah4\", \"Blah5\" );
// init counter to zero
$i=0;
// loop while there is a question and answer
while (isset($question[$i]) && isset($answer[$i])) {
// \"Question 1: This Quiz is made by who?\"
// \"Your Answer: John Doe\"
// \"Correct Answer: Owen\"
print (\"Question \".($i+1).\": $question[$i]\n\");
print (\"Your Answer: \".${'q'.($i+1)}.\"\n\");
print (\"Correct Answer: $answer[$i]\n\");
print (\"Result: \");
// IF the answer for the current question EQUALS the answer provided in the
// form THEN
//
// to be even more detailed....
// ${'q'.($i+1)} is a dynamic variable
// when on quesion 1, the valiable being accessed will be $q1
// when on question 2, it will be $q2
// etc., etc., so on, and so forth
// when on question 5, it will be $q5
if ($answer[$i] == ${'q'.($i+1)}) {
print (\"You got it right!\");
// increment counter for correct answers
$cnt_right++;
}
else {
print (\"You got it wrong!\");
// increment counter for wrong answers
$cnt_wrong++;
}
$i++;
} // loop
print (\"Final Results:\n\");
print (\"Total Correct: $cnt_right\n\");
print (\"Total Wrong: $cnt_wrong\n\");
?>
Mark Hensler
If there is no answer on Google, then there is no question.
nuk3 posted this at 08:45 — 20th November 2002.
They have: 238 posts
Joined: May 2002
Ok I think I understand it now. There are just a few new things that you are using above which I've never seen before.. Thanks allot for your help Mark
Suzanne posted this at 14:34 — 20th November 2002.
She has: 5,507 posts
Joined: Feb 2000
Mark, you'd be an excellent teacher. Scratch that, you ARE an excellent teacher.
nuk3 posted this at 00:39 — 23rd November 2002.
They have: 238 posts
Joined: May 2002
Yes he is a good teacher!
TonyMontana posted this at 06:41 — 24th November 2002.
They have: 218 posts
Joined: Apr 2001
$a + $a = "$total";
$total first needs to be defined, 'given a value', and since it needs to be evaluated, omit the quotation marks, ie:
<?php
$a = 6;
$total = $a + $a;
echo $total;
?>
Cheers,
TonyMontana
nuk3 posted this at 04:48 — 25th November 2002.
They have: 238 posts
Joined: May 2002
Ok thanks for that..
Want to join the discussion? Create an account or log in if you already have one. Joining is fast, free and painless! We’ll even whisk you back here when you’ve finished.