-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.

u_mulder
  • 54,101
  • 5
  • 48
  • 64

1 Answers1

0

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.