Welcome to the Treehouse Community
Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.
Looking to learn something new?
Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.
Start your free trial
Andrew McCombs
4,309 PointsPHP - If statements with || - Need help
I don't understand why this doesn't work.
if I run this, it redirects me to the protected.php.
Also, in your experience. Is this the best way to check this?
function admin_protect($user_ok, $log_userlevel) {
if ($user_ok === true) { //This is checking to make sure the user is logged in - just in case.
if ($log_userlevel !== '7' || $log_userlevel !== '8') {
header('Location: protected.php');
exit();
}
}
}
if I run it like this then it works.
function admin_protect($user_ok, $log_userlevel) {
if ($user_ok === true) {
if ($log_userlevel !== '7') {
header('Location: protected.php');
exit();
}
}
}
//or like this
function admin_protect($user_ok, $log_userlevel) {
if ($user_ok === true) {
if ($log_userlevel !== '8') {
header('Location: protected.php');
exit();
}
}
}
3 Answers
Tariq Naeem
281 PointsPut && instead of || and use != instead of !==
Ricardo Hill-Henry
38,443 PointsI believe you're experiencing what's called "short circuiting." In the case of using ||, the system doesn't check to see if both are true. It gets to the first, checks $login_userlevel !=='7', and if that returns true it doesn't continue to the next condition because a true value has already been found. In that case, if $login_userlevel !== '7' always returns true, then $login_userlevel !== '8' will never be checked. In this case, there is a chance that a value of '8' could slip through, so it would make more sense to use &&. Using && says I want to make sure both conditions are true before executing the code below.
Andrew McCombs
4,309 PointsBut if I use the && they both have to be true
I only need one of them true
you are going to be either level 7 or level 8, your your redirected.
thomascawthorn
22,986 PointsThe first very important thing to realize is that multiple conditionals do not relate to each other at all.
This statement
<?php
($log_userlevel !== '7' || $log_userlevel !== '8')
if the user is not level 7 continue OR if the user is not level 8 continue.
As Ricardo Hill-Henry mentioned, PHP will work through the statements in order.
If the user is level 8, the first statement will pass, because this user is not level 7 and the 'if' block will be run. The same applies if the user is level 8.
You need to use an && statement, because both your conditionals need to be true to pass. i.e. the user should not be level 7 AND should not be level 8. Using && means both statements need to return true before the if block can be run.
Now, this is so because you're avoiding a negative. If you were to be choosing an action which depended on the user being 7 or 8, then && wouldn't make sense - because a user can only be one level.
<?php
($log_userlevel == '7' && $log_userlevel == '8') {
// This code won't run
}
In this case, you would be right to check for OR
<?php
($log_userlevel == '7' || $log_userlevel == '8') {
// Level 7 or level 8 users would be redirected.
}
If you're testing for the negative - i.e. only people NOT level 7 or 8 get redirected
<?php
($log_userlevel !== '7' && $log_userlevel !== '8') {
// Users who are both NOT level 7 AND NOT level 8 will be redirected.
}
Here you're testing for a negative OR
<?php
($log_userlevel !== '7' || $log_userlevel !== '8') {
// Users who are NOT level 7 (i.e. could be level 8 will get redirected)
// Users who are NOT level 8 (i.e. could be level 7 will get redirected)
}
Work through this enough times and it should make a bit more sense ;)