dtv7174 2017-02-17 21:20
浏览 62

PHP条件语句不起作用

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

  • 写回答

3条回答 默认 最新

  • douqiang6448 2017-02-17 21:26
    关注

    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);
    
    评论

报告相同问题?

悬赏问题

  • ¥20 BAPI_PR_CHANGE how to add account assignment information for service line
  • ¥500 火焰左右视图、视差(基于双目相机)
  • ¥100 set_link_state
  • ¥15 虚幻5 UE美术毛发渲染
  • ¥15 CVRP 图论 物流运输优化
  • ¥15 Tableau online 嵌入ppt失败
  • ¥100 支付宝网页转账系统不识别账号
  • ¥15 基于单片机的靶位控制系统
  • ¥15 真我手机蓝牙传输进度消息被关闭了,怎么打开?(关键词-消息通知)
  • ¥15 装 pytorch 的时候出了好多问题,遇到这种情况怎么处理?