PHP内部访问功能包括

I am having a problem accessing a function result from inside an include. I have tried to search an answer for this but "include inside of function" dominates the landscape of that query.

The following code works (including only necessary information). There are two main links, and depending on which page your on, the relevant link will be underlined and the other not:

index.php

if (basename($_SERVER['PHP_SELF']) == "index.php") {
    $nav1 = "navselected";
    $nav2 = "navnone";
} 
else {
    $nav1 = "navnone";
    $nav2 = "navselected";
}

include "/...../public_html/includes/header.php";

header.php

<div class="nav1">
    <a href="index.php" class="navfont" id="<?php echo $nav1; ?>">Home</a>
</div>
<div class="nav2">
    <a href="marketmainstreet.php" class="navfont" id="<?php echo $nav2; ?>">Browse Market</a>
</div>

The following code does not work:

functions.php

function underlineActivePage()
{
    if (basename($_SERVER['PHP_SELF']) == "index.php") {
        $nav1 = "navselected";
        $nav2 = "navnone";
    } else {
        $nav1 = "navnone";
        $nav2 = "navselected";
    }
}

index.php

include "/...../public_html/includes/functions.php";
underlineActivePage();
include "/...../public_html/includes/header.php";

header.php

<div class="nav1">
    <a href="index.php" class="navfont" id="<?php echo $nav1; ?>">Home</a>
</div>
<div class="nav2">
    <a href="marketmainstreet.php" class="navfont" id="<?php echo $nav2; ?>">Browse Market</a>
</div>

The variable results return empty. Know that functions used on every other part of the page work, it is only in this instance when the variable needs to be accessed inside the include that it does not. From what I understand, and has worked for me thus far, is that by including a file like so:

"/...../public_html/includes/header.php"

adds the code to the page as if it were a part of the page, as opposed to just adding the result of the code to the page. I can access other variables this way on my header.php, just not the ones coming from the function include. I'm actually pretty new to incorporating functions in my layout ("lazily" repeated code but I'm changing that) Thanks for our help with this.

The variables $nav1 and $nav2 are set in the function scope, then used in the global scope. To make this work you could declare $nav1 and $nav2 as globals inside the function.

function underlineActivePage() {
  global $nav1, $nav2;

  if (basename($_SERVER['PHP_SELF']) == "index.php") {
    $nav1 = "navselected";
    $nav2 = "navnone";
  } 
  else {
    $nav1 = "navnone";
    $nav2 = "navselected";
  }

}

Here is a very simple solution for your problem, (EDIT: As I am assuming that you want to display the link as selected i.e. underlined or bold in the navigation bar or something)

define a php variable and store your current page in it as:

$current_page_name = substr($_SERVER["SCRIPT_NAME"],strrpos($_SERVER["SCRIPT_NAME"],"/")+1);

embed conditional php in your html and write a selected class in your CSS:

<li><a <?php if($current_page_name=="index.php"){echo " class=\"selected\"";}?> href="index.php">Enter the value you want to display</a></li>
<li><a <?php if($current_page_name=="marketmainstreet.php"){echo " class=\"selected\"";}?> href="marketmainstreet.php">Enter another value</a></li>

This is a scoping issue. However, instead of going "global", I recommend returning values from the function. Whenever possible, avoid global variables (as I mentioned in the other comments, especially for a team development, global variables are dangerous).

Change functions.php to:

function underlineActivePage()
{
    if (basename($_SERVER['PHP_SELF']) == "index.php") {
        $nav[] = "navselected";
        $nav[] = "navnone";
    } else {
        $nav[] = "navnone";
        $nav[] = "navselected";
    }
    return $nav;
}

and index.php to :

include "/...../public_html/includes/functions.php";
$nav = underlineActivePage();
include "/...../public_html/includes/header.php";

and header.php to:

<div class="nav1">
<a href="index.php" class="navfont" id="<?php echo $nav[0]; ?>">Home</a>
</div>
<div class="nav2">
<a href="marketmainstreet.php" class="navfont" id="<?php echo $nav[1]; ?>">Browse Market</a>
</div>