Myles Malloy Myles Malloy - 1 year ago 71
PHP Question

PHP OR statement not working

I am trying to create a function that checks part of the URL on a page and then either shows or hides a nav bar based on the results. I can get the function to work if it checks for only one string but adding different strings in an OR statement doesn't do anything.

Below is an example of the function working with a single condition...

<?php $url = 'http://' . $_SERVER['SERVER_NAME'] . $SERVER['REQUEST_URI'];
if (strpos($url,'/login') == false){ ?>


<?php } ?>

This correctly prevents the navbar from showing up on the "login" page. But if I also want the navbar to not show up on the "register" page and do something like this...

<?php $url = 'http://' . $_SERVER['SERVER_NAME'] . $SERVER['REQUEST_URI'];
if (strpos($url,'/login') == false || strpos($url,'/register') == false){ ?>


<?php } ?>

...the functionality stops working entirely and the navbar shows up on all pages.
So how can I refine my OR statement so that it works properly? Thanks!

Answer Source

Logic flaw. Change the or condition to and

if(strpos($url,'/login')==false || strpos($url,'/register')==false)

Should be

if(strpos($url,'/login')===false && strpos($url,'/register')===false)