Myles Malloy Myles Malloy - 4 months ago 8
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){ ?>

<nav>Navbar</nav>

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

<nav>Navbar</nav>

<?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

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