wtfsmd Posted February 21, 2009 Share Posted February 21, 2009 Hey guys i have a quick question about a conditional statement. Here is the code: if($_SESSION['userrank'] >= "3" || $_SESSION['username'] == $row['t_op']){ echo "I am allowed to do stuff"; }else{ echo "I am not allowed to do stuff =("; } It works if i am logged in on my admin account because my user rank is 4 But if i try to do something on my other account that should equal to the second conditional statement ($_SESSION['username'] == $row['t_op']) I get the "I am not allowed to do stuff". Is there something wrong with my syntax or have i made a rook mistake somewhere? Thanks for any help. Quote Link to comment Share on other sites More sharing options...
btherl Posted February 21, 2009 Share Posted February 21, 2009 The statement looks fine to me. Try this: print "Comparing {$_SESSION['username']} with {$row['t_op']}<br>"; if($_SESSION['userrank'] >= "3" || $_SESSION['username'] == $row['t_op']){ echo "I am allowed to do stuff"; }else{ echo "I am not allowed to do stuff =("; } Quote Link to comment Share on other sites More sharing options...
Lodius2000 Posted February 21, 2009 Share Posted February 21, 2009 to be extra sure put parentheses around your individual expressions, I dont think it will work but its good practice too what is the value of $row['t_op']? Quote Link to comment Share on other sites More sharing options...
wtfsmd Posted February 21, 2009 Author Share Posted February 21, 2009 Found where i messed up, spelling error Got it to work fine, could you elaborate on the good practice with parentheses around my individual expressions? like just a little example or something. Thanks for your help. Quote Link to comment Share on other sites More sharing options...
Lodius2000 Posted February 21, 2009 Share Posted February 21, 2009 well php follows order of operations just like they teach in algebra 1, so 5+5*3 is going to be 20 not 30 (30 would be if you wen left to right and added 5+5 and then multiplied by 3). But what happens if you want the result to be 30... (5+5)*3 = 30, php does the same thing with all sorts of operators. mathematical, comparison, logical... and it is pretty good at separating expressions but it is not foolproof, nor is the person 3 years from now, looking at your code and trying to figure out what your hugely long if() condition means. While there is little chance that anyone but the most novice programmer, and definately not php will get screwed up with your expressions from above, consider this <?php if($_SESSION['userrank'] >= 3 && $_SESSION['userrank'] != 8 || $_SESSION['username'] == $row['t_op']) ?> makes you go huh? for a second doesnt it, we know that we want the first condition to be true (at least a rank of 3) but then it gets fishy. if the rank is 8 does username have to be = to $row['t_op'], and vice versa, or does if the rank is 7 but username isnt = to $row['t_op'] do we still get in you can see where a human can get befuddled for a while, in this example, php probably wouldnt get confused, but look how much clearer it is when you add a couple more parentheses: <?php if( ($_SESSION['userrank'] >= 3 && $_SESSION['userrank'] != 8 ) || ($_SESSION['username'] == $row['t_op']) ) ?> now we know for sure (and php will also know for sure) that either rank needs to be 3 or above but not 8 OR username = $row['t_op'], now there is no ambiguity Quote Link to comment Share on other sites More sharing options...
btherl Posted February 22, 2009 Share Posted February 22, 2009 My approach to such conditionals (anything which is not immediately understandable) is to factor it out like this: $rank_test = $_SESSION['userrank'] >= 3 && $_SESSION['userrank'] != 8; $username_test = $_SESSION['username'] == $row['t_op']; if ($rank_test || $username_test) { Quote Link to comment Share on other sites More sharing options...
Cal Posted February 22, 2009 Share Posted February 22, 2009 That's a cool way of doing it : o Quote Link to comment Share on other sites More sharing options...
wtfsmd Posted February 23, 2009 Author Share Posted February 23, 2009 well php follows order of operations just like they teach in algebra 1, so 5+5*3 is going to be 20 not 30 (30 would be if you wen left to right and added 5+5 and then multiplied by 3). But what happens if you want the result to be 30... (5+5)*3 = 30, php does the same thing with all sorts of operators. mathematical, comparison, logical... and it is pretty good at separating expressions but it is not foolproof, nor is the person 3 years from now, looking at your code and trying to figure out what your hugely long if() condition means. While there is little chance that anyone but the most novice programmer, and definately not php will get screwed up with your expressions from above, consider this <?php if($_SESSION['userrank'] >= 3 && $_SESSION['userrank'] != 8 || $_SESSION['username'] == $row['t_op']) ?> makes you go huh? for a second doesnt it, we know that we want the first condition to be true (at least a rank of 3) but then it gets fishy. if the rank is 8 does username have to be = to $row['t_op'], and vice versa, or does if the rank is 7 but username isnt = to $row['t_op'] do we still get in you can see where a human can get befuddled for a while, in this example, php probably wouldnt get confused, but look how much clearer it is when you add a couple more parentheses: <?php if( ($_SESSION['userrank'] >= 3 && $_SESSION['userrank'] != 8 ) || ($_SESSION['username'] == $row['t_op']) ) ?> now we know for sure (and php will also know for sure) that either rank needs to be 3 or above but not 8 OR username = $row['t_op'], now there is no ambiguity Thanks that was a pretty clear example you provided, definitely makes more sense and is easier to read when you do it that way. Quote Link to comment 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.