doumu1951 2018-03-06 09:54
浏览 83
已采纳

PHP:Strcmp返回值-58,即使2个字符串看起来相同?

I'm making a website. On the website I have navigation bar which shows pages that a user can click on, and highlights the current active page the user is on. That navigation bar is its own php file that is imported once into the 2 pages of the website - index.php and login.php. Just like what this stackoverflow question is trying to do.

/* 
* This function checks to see if the page name given is the active page name.
* If yes, it gives it the class="active" attribute to highlight to the user that said script is the active page.
*
* @param string $scriptname The name of the php script.
* @return void
*/
function echoActiveClassIfPageIsActive($scriptname)
{
    $current_file_name = $_SERVER['SCRIPT_NAME'];

    if (strcmp($current_file_name, $scriptname) == 0)
        echo 'class="active"';
}

This function is called in the php code embedded into HTML below.

<ul>
<li><a href='index.php' <?=echoActiveClassIfPageIsActive('index.php')?> >Home</a></li>
<li><a href='login.php' <?=echoActiveClassIfPageIsActive('login.php')?> >Login</a></li>

Expected output: If I am on localhost.com/index.php, the tag with index.php should be given the "active" class (I defined the class "active" in css to be highlighted).

Actual output (Problem): But everytime I run the program it fails to highlight the active page, regardless of whether I am on page index.php or login.php. So surely that means strcmp determined that $current_file_name does not equal $scriptname.

I tried adding

echo $_SERVER['SCRIPT_NAME'];
echo strcmp($current_file_name, $scriptname);

to understand what $_SERVER['SCRIPT_NAME'] and strcmp returned. This is the output:

<a href="index.php" index.php-58="">Home</a>

On index.php, they returned "index.php" and "-58" respectively.It shocked me. Why would comparing "index.php" and "index.php" result in a strcmp return value of -58?

When I changed the code to

/* 
* This function checks to see if the page name given is the active page name.
* If yes, it gives it the class="active" attribute to highlight to the user that said script is the active page.
*
* @param string $scriptname The name of the php script.
* @return void
*/
function echoActiveClassIfPageIsActive($scriptname)
{
    $current_file_name = basename($_SERVER['SCRIPT_NAME'],".php");

    if (strcmp($current_file_name, $scriptname) == 0)
        echo 'class="active"';
}

and

    <ul>
<li><a href='index.php' <?=echoActiveClassIfPageIsActive('index')?> >Home</a></li>
<li><a href='login.php' <?=echoActiveClassIfPageIsActive('login')?> >Login</a></li>

the code worked fine (ie the active page was highlighted). This is the output:

<a href="index.php" class="active">Home</a>

I'm very confused as to why it works now. I understand how basename() works. It returns only trailing name component of path. But here, as shown by <a href="index.php" index.php-58="">Home</a> , even without basename(), the 2 strings look identical.

Could it be that without basename(), $_SERVER['SCRIPT_NAME'] returns "/index.php"? Afterall, SCRIPT_NAME "contains the current script's path", just that the "/" is invisible and not seen when echoed? So I used the online php function tester to do strcmp("/index.php","index.php") . It returned a value of 1, not -58.

Could anyone please help me understand why strcmp gave me -58?? Thank you.

  • 写回答

1条回答 默认 最新

  • dongyo1959 2018-03-07 12:53
    关注

    Credits to @CBroe who introduced var_dump to me! :)

    I added the following lines of code

    var_dump($current_file_name, $scriptname);
    var_dump(basename($_SERVER['SCRIPT_NAME'],".php"));
    

    into my function echoActiveClassIfPageIsActive.

    function echoActiveClassIfPageIsActive($scriptname)
    {
        $current_file_name = $_SERVER['SCRIPT_NAME'];
        var_dump($current_file_name, $scriptname);
        var_dump(basename($_SERVER['SCRIPT_NAME'],".php"));
        if (strcmp($current_file_name, $scriptname) == 0)
            echo 'class="active"';
    }
    

    Turns out,

    • $current_file_name was string(10)=" index.php"
    • $scriptname was string(9)="index.php"

    Hence, strcmp($current_file_name, $scriptname) did not return a value of 0. The 2 strings were not identical, unlike what I thought. basename($current_file_name) and $scriptname are the identical strings.

    This is how I realised my error. Just thought I might share this thought process.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 对于相关问题的求解与代码
  • ¥15 ubuntu子系统密码忘记
  • ¥15 信号傅里叶变换在matlab上遇到的小问题请求帮助
  • ¥15 保护模式-系统加载-段寄存器
  • ¥15 电脑桌面设定一个区域禁止鼠标操作
  • ¥15 求NPF226060磁芯的详细资料
  • ¥15 使用R语言marginaleffects包进行边际效应图绘制
  • ¥20 usb设备兼容性问题
  • ¥15 错误(10048): “调用exui内部功能”库命令的参数“参数4”不能接受空数据。怎么解决啊
  • ¥15 安装svn网络有问题怎么办