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

问题描述 投票:-2回答:1

我正在建立一个网站。在网站上,我有导航栏,显示用户可以点击的页面,并突出显示用户所在的当前活动页面。该导航栏是它自己的php文件,它被导入网站的2个页面 - index.php和login.php。就像this stackoverflow question试图做的那样。

/* 
* 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"';
}

这个函数在嵌入HTML下面的php代码中调用。

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

预期输出:如果我在localhost.com/index.php上,带有index.php的标签应该被赋予“active”类(我在css中定义了“active”类以突出显示)。

实际输出(问题):但是每次运行程序时都无法突出显示活动页面,无论我是在页面index.php还是login.php。所以肯定这意味着strcmp确定$ current_file_name不等于$ scriptname。

我尝试添加

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

了解什么$_SERVER['SCRIPT_NAME']strcmp返回。这是输出:

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

在index.php上,他们分别返回了“index.php”和“-58”。这让我很震惊。为什么比较“index.php”和“index.php”导致strcmp返回值为-58?

当我将代码更改为

/* 
* 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"';
}

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

代码工作正常(即活动页面突出显示)。这是输出:

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

我很困惑为什么它现在有效。我理解basename()是如何工作的。它仅返回路径的尾随名称组件。但是在这里,如<a href="index.php" index.php-58="">Home</a>所示,即使没有basename(),2个字符串看起来也一样。

可能是没有basename(),$ _SERVER ['SCRIPT_NAME']会返回“/index.php”吗?毕竟,SCRIPT_NAME "contains the current script's path",只是“/”是看不见的,并且在回声时看不见?所以我用在线php功能测试器来做strcmp("/index.php","index.php")。它返回值1,而不是-58。

谁能帮助我理解为什么strcmp给了我-58 ??谢谢。

php html
1个回答
0
投票

归功于@CBroe,他向我介绍了var_dump! :)

我添加了以下代码行

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

进入我的功能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"';
}

原来,

  • $current_file_namestring(10)=" index.php"
  • $scriptnamestring(9)="index.php"

因此,strcmp($current_file_name, $scriptname)没有返回值0.这两个字符串不相同,不像我想的那样。 basename($current_file_name)$scriptname是相同的字符串。

这就是我意识到错误的方式。我以为我可能会分享这个思考过程。

© www.soinside.com 2019 - 2024. All rights reserved.