bob jomes
bob jomes

Reputation: 281

Or operator not displaying content

Im trying to get something to be echoed if the user is on one of the two pages, but it isn't echoed

if(stripos($_SERVER['REQUEST_URI'], 'user.php') || ($_SERVER['REQUEST_URI'], 'message.php')) {
echo 'hello';
}

Upvotes: -1

Views: 50

Answers (3)

Brian
Brian

Reputation: 1025

Wrap each conditional argument in its own block with parenthesis. Every function added to the condition must include 2 parenthesis.

IF ( ($a == $x) or ($b == $y) ) {}

if (
    (stripos($_SERVER['REQUEST_URI'], 'user.php')) ||
    (stripos($_SERVER['REQUEST_URI'], 'message.php')) 
) {
  echo 'hello';
}

Upvotes: 0

Rajdeep Paul
Rajdeep Paul

Reputation: 16963

First of all, $_SERVER['REQUEST_URI'] will give you the URI which was given in order to access this page, not the actual page name. Use basename($_SERVER['PHP_SELF']) to get the actual page name.

And second, the condition of your if clause is also wrong. From the manual of stripos() function:

Returns the position of where the needle exists relative to the beginnning of the haystack string (independent of offset). Also note that string positions start at 0, and not 1.

Returns FALSE if the needle was not found.

So both of your conditions will fails if the needle matches exactly with the haystack. Instead check the condition like this:

if(stripos($haystack, $needle) !== false || stripos(stripos($haystack, $needle)) !== false) {
    echo 'hello';
}

So the solution is like this:

if(stripos(basename($_SERVER['PHP_SELF']), 'user.php') !== false || stripos(basename($_SERVER['PHP_SELF']), 'message.php') !== false) {
    echo 'hello';
}

Upvotes: 1

R Singh
R Singh

Reputation: 765

if(stripos($_SERVER['REQUEST_URI'], 'user.php') || stripos($_SERVER['REQUEST_URI'], 'message.php')) {
echo 'hello';
}

Notice the stripos function in the clause after ||.

Upvotes: 0

Related Questions