tags:

views:

45

answers:

2

Hello,

I'm trying to use and OR statement in php 5 but it's just not working, what's wrong with this script. It's suppose to fire the error but it doesn't. If I remove the "||"(what I thought was the OR statement) it fires the error.

if($winner=="2" && $fteamscore>$steamscore){
$return['error'] = true;
$return['msg'] = 'Wrong Score, You said you LOST but you are reporting that you WON.';
}
if($winner=="3" && $fteamscore>$steamscore){
$return['error'] = true;
$return['msg'] = 'Wrong Score, You said you WON but your are reporting that your OPPONENT won.';
}
if($fteamscore<$steamscore && $winner=="3" || $winner=="2"){
+2  A: 

Perhaps you need parens?

if( $fteamscore<$steamscore && ($winner=="3" || $winner=="2" ) ) {

This way that expression in parens is evaluated before being &&'d due to operator precedence.

meder
The answers are always so freaking simple. Thanks bro
John Sims
+1  A: 

Your brackets are wrong. Try this:

($fteamscore<$steamscore) && ($winner=="3" || $winner=="2")

Without any brackets, the "and" is evaluated first. Your expression is equivalent to:

(($fteamscore<$steamscore && $winner=="3") || $winner=="2")

More on operator precedence: http://en.wikipedia.org/wiki/Logical_connective#Order_of_precedence

FrustratedWithFormsDesigner
Your answer worked best. Thanks
John Sims