0

I have a statement that checks the page's url and marks up a page accordingly, but it only works when my if statement has one option to check for.

$url = 'http://' . $_SERVER['SERVER_NAME'] . $_SERVER['REQUEST_URI'];

<?php if (strpos($url, 'events/eventname')!= false) { ?>
    ~markup~
<? } ?>

If I modify it to check for two possible urls...

<?php if (strpos($url, 'events/eventname')!= false) { ?>
    ~markup~
<? }else if (strpos($url, 'events/othereventname')!= false) { ?>
    ~markup~
<? } ?>

... the page won't load. I must be missing something obvious- can someone tell me what is wrong with this function?

*edit: Since it was requested I have included the $url variable and more specific url examples

13
  • 1
    Where is URL defined? And also, what are those "urlhere" or "otherurlher"? These informations may usefull for devs to understand your code. Commented Feb 17, 2017 at 21:22
  • Try !== instead of !=. Regarding PHP documentation: Warning This function may return Boolean FALSE, but may also return a non-Boolean value which evaluates to FALSE. Please read the section on Booleans for more information. Use the === operator for testing the return value of this function. (php.net/manual/en/function.strpos.php) Commented Feb 17, 2017 at 21:24
  • @Ad5001 Gameur codeur autre URL is defined elsewhere in the code- I didn't include it or the actual urls (which I substituted with urlhere and otherurlhere) because it wasn't directly relevant to the problem. Commented Feb 17, 2017 at 21:32
  • I agree that some more realistic examples for $url and 'urlhere' could be helpful here. I don't necessarily agree that it isn't directly relevant to the problem. Commented Feb 17, 2017 at 21:32
  • I think it looks like you might have the arguments to strpos backward, but without really seeing what's what, that's just a guess. Commented Feb 17, 2017 at 21:34

3 Answers 3

2

strpos returns 0 when search substring is in the beginning of the query string. You can replace != to !== to make it work - otherwise php is internally transforming false to zero, which leads to incorrect comparison result.

For example:

<?php
var_dump(strpos('aaa', 'a'));
echo var_dump(strpos('aaa', 'a') === false);
echo var_dump(strpos('aaa', 'a') == false);
Sign up to request clarification or add additional context in comments.

Comments

1

Try to use !== comparison just just in case string is at position 0.

Another syntax problem is else if, while you should use elseif.

Try also changing short php tag <? to full one <?php.

3 Comments

Thanks for the answer Chris Cynarski but neither of those corrections solved the problem.
Try using full php tags <?php instead of short one <?.
If short tags weren't enabled, the first one wouldn't have worked either.
-2

Rather than using the strpos() you can get the request uri which is anything after the domain name (ie: www.example.com/foo/bar would give you /foo/bar).

$url = $_SERVER['REQUEST_URI'];

if($url == "/foo/bar") {
    // markup
} elseif($url == "/bar/foo") {
    // markup
} else {
    // markup
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.