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

PHP

If Statement Help - PHP

I don't understand how to run this the right way, what I want it if the first part of the if statement isnt trun then see if the second on is.

if (($user_ok === true && $log_username === $post_author) && ($user_ok === true && $log_userlevel === 7 && $log_userlevel === 8)){
                echo $editPostbtn;
            }

2 Answers

Timothy Rimnac
Timothy Rimnac
26,219 Points

I'm not exactly sure what you're asking but I'm assuming that you want to check if one OR the other condition is true. Using the '&&' means that both the first AND the second condition is true. You'll want to use the OR comparator also known in PHP as the double pipe, '||'.

Try running the code as

if (($user_ok === true && $log_username === $post_author) || ($user_ok === true && $log_userlevel === 7 && $log_userlevel === 8)){
                echo $editPostbtn;
            }

Notice that the '&&' between the two conditionals was replaced with '||', also known as the OR comparator.

Bill Hinostroza
Bill Hinostroza
19,273 Points

I don't know if this is what you're looking but it was worth giving it a try.

if ($user_ok === true && $log_username === $post_author){
   echo $editPostbtn;
} 
elseif($user_ok === true && $log_userlevel === 7 && $log_userlevel === 8)){
    echo "Whatever";          
 }
else{
  echo "Please input the correct information."
}