icor1031 Posted September 18, 2014 Share Posted September 18, 2014 (edited) This is what I want: If comparisonTimeH isn't zero, I want it to check if comparisonTimeH is zero, and comparisonTimeM is larger than or equal to 2. If so, I want it to pass and run the script. This is what I have, and it's not right. It runs the else statement, echo test 2. if ($comparisonTimeH != 0 || $comparisonTimeH = 0 AND $comparisonTimeM >= 1) { echo "test"; } else { echo "test2"; } echo "<br />" . $comparisonTimeH . " " . $comparisonTimeM; My last echo, the one outside of the if statements - it returns 0 and 12. Thus, both conditions that I'm trying to get, are true. My if's logic is clearly wrong, and I can't figure out what operators to use. If I change the AND to &&, it breaks and my final echo doesn't return comparisonTimeH (it's blank). Please teach me, thanks! Edited September 18, 2014 by icor1031 Quote Link to comment https://forums.phpfreaks.com/topic/291140-basic-logic/ Share on other sites More sharing options...
Solution Jacques1 Posted September 18, 2014 Solution Share Posted September 18, 2014 (edited) You're using the assignment operator “=” instead of the equality operator “==”, and you've confused yourself with exotic precedence rules. Fix the operator, and use parentheses to define the order of evaluation. When you find yourself juggling with different variations of the logical “and” operator, that's a sure sign you need to stop. The condition can also be simplified to $comparisonTimeH != 0 || $comparisonTimeM >= 1 If $comparisonTimeH != 0 is false, then $comparisonTimeH == 0 is automatically true, so no need to write that down. Edited September 18, 2014 by Jacques1 Quote Link to comment https://forums.phpfreaks.com/topic/291140-basic-logic/#findComment-1491466 Share on other sites More sharing options...
jazzman1 Posted September 18, 2014 Share Posted September 18, 2014 (edited) You're using the assignment operator “=” instead of the equality operator “==” He wants to use assignment operator there,it's a tricky question. In the first example, we've got - "true" in the second one - "false". $var1 = FALSE || TRUE; $var2 = FALSE OR TRUE; The answer is precedence. @OP, never ever mix up the logical operators written in pure English words with those one written with symbols. If you want to separate the logic in your application always use parentheses! Edited September 18, 2014 by jazzman1 Quote Link to comment https://forums.phpfreaks.com/topic/291140-basic-logic/#findComment-1491471 Share on other sites More sharing options...
Jacques1 Posted September 18, 2014 Share Posted September 18, 2014 He wants to use assignment operator there No, he or she doesn't. Read the question again: The goal is to make an equality check. @OP, never ever mix up the logical operators written in pure English words with those one written with symbols. If you want to separate the logic in your application always use parentheses! Yes, I already said that. Quote Link to comment https://forums.phpfreaks.com/topic/291140-basic-logic/#findComment-1491473 Share on other sites More sharing options...
icor1031 Posted September 18, 2014 Author Share Posted September 18, 2014 Yes, I was confused with the precedence. And it was quite a silly mistake to use assignment . Thanks! You're using the assignment operator “=” instead of the equality operator “==”, and you've confused yourself with exotic precedence rules. Fix the operator, and use parentheses to define the order of evaluation. When you find yourself juggling with different variations of the logical “and” operator, that's a sure sign you need to stop. The condition can also be simplified to $comparisonTimeH != 0 || $comparisonTimeM >= 1 If $comparisonTimeH != 0 is false, then $comparisonTimeH == 0 is automatically true, so no need to write that down. Quote Link to comment https://forums.phpfreaks.com/topic/291140-basic-logic/#findComment-1491475 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.