Jump to content

Conditional Statements and Operators


wtfsmd

Recommended Posts

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.

Link to comment
Share on other sites

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 =(";
}

Link to comment
Share on other sites

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

Link to comment
Share on other sites

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) {

 

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.