davidcriniti Posted April 21, 2010 Share Posted April 21, 2010 Hi, I've made an online test for my kids at school, which is marked automatically via php. Most questions have been ok, using this sort of format: if ($q001 == "right") {$q001score = 1;}else {$q001score = 0;}; However, I've had a bit of trouble with questions where there is more than one answer. I've tried using 'or' with little success. I find that '1' gets added to the score regardless of whether I get the right answer, wrong answer, or input no answer when using the following script: if ($q001 == "right" or "right2") {$q001score = 1;}else {$q001score = 0;}; I've also substituted || for 'or' with no success. I have solved the problem by extending the if-else statement as follows: if ($q001 == "right" ) {$q001score = 1;} else if ($q001 == "right2") {$q001score = 1;}else {$q001score = 0;}; I was just wondering if I could reduce the amount of code by altering the 'or' statement above as the if, else if, else statement seems a bit cumbersome? Yours gratefully, Dave Quote Link to comment https://forums.phpfreaks.com/topic/199241-or-vs-if-else/ Share on other sites More sharing options...
grim1208 Posted April 21, 2010 Share Posted April 21, 2010 you were close, try this if (($q001 == "right") || ($q001 == "right2")) { $q001score = 1; } else { $q001score = 0; } Quote Link to comment https://forums.phpfreaks.com/topic/199241-or-vs-if-else/#findComment-1045688 Share on other sites More sharing options...
oni-kun Posted April 21, 2010 Share Posted April 21, 2010 The OR and || operators are both correct. You are not following the statement though, Consider this correction: $q001 == "right" || $q001 == "right2" You can only use another statement, You cannot use "This or that", it has to be "if this = this OR this = that" If you wish for a more "automated" method, you can define an array. $correct_answers = Array('right', 'right2'); if (in_array($q001, $correct_answers)) { $q001score = 1; } else { $q001score = 0; } This will save you from typing a pile of IF/AND/OR statements provided you add to the array. EDIT: Updated another method. Quote Link to comment https://forums.phpfreaks.com/topic/199241-or-vs-if-else/#findComment-1045689 Share on other sites More sharing options...
Mchl Posted April 21, 2010 Share Posted April 21, 2010 if($q001 == "right" || $q001 == "right2") {} or if(in_array($q001,array("right","right2"))) {} Quote Link to comment https://forums.phpfreaks.com/topic/199241-or-vs-if-else/#findComment-1045690 Share on other sites More sharing options...
davidcriniti Posted April 21, 2010 Author Share Posted April 21, 2010 That was quick! Thanks everyone! A very grateful, Dave Quote Link to comment https://forums.phpfreaks.com/topic/199241-or-vs-if-else/#findComment-1045697 Share on other sites More sharing options...
ignace Posted April 21, 2010 Share Posted April 21, 2010 Just to note that: $q001 == "right" or "right2" works but is not the desired result as this always executes ($q001score = 1) never ($q001score = 0) because "right2" equals to true and because of the nature-of (or, ||) statement (which executes the if-body whenever a, b or both are true) Quote Link to comment https://forums.phpfreaks.com/topic/199241-or-vs-if-else/#findComment-1045716 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.