0

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.

3
  • 1
    php.net/manual/en/language.variables.scope.php Commented Aug 21, 2014 at 19:28
  • I don't see you setting a $_SESSION with session_start(). Maybe it wasn't included in the code for display purposes, but that would give you empty variables. Commented Aug 21, 2014 at 19:29
  • Specifically, even though you've named your variables the same, the $nav1 and $nav2 defined within underlineActivePage() doesn't know about the $nav1 and $nav2 within the index.php page. Commented Aug 21, 2014 at 19:29

3 Answers 3

1

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";
  }

}
Sign up to request clarification or add additional context in comments.

5 Comments

I recommend returning variables, rather than using global variables. In a team development environment, global variables are dangerous. Please see my answer.
I definitely agree that global variables should be avoided. I would note though that you answer doesn't eliminate the use of globals, it just reduces them from 2 ($nav1 and $nav2) to 1 ($nav).
Thanks for the responses, these both answer my question in a different way. Hard to select the right answer? As my environment won't involve a team of developers I'll use global for now. Thanks again.
@TylerMarien, $nav isn't global, because if you included index.php into another file, it would not be in the scope. Also, it wouldn't be available in any functions included prior to the declaration line. But the point is, anyone editing index.php wouldn't have to go far to figure out what $nav is or how it is derived.
In the example, $nav is being declared in the global scope (ie. not inside of a function) and therefore is global. You would be able to access $nav in any file/function that you included index.php. I agree that you solution is clearer as to where the variable is getting set, as it is one step closer to where it is being used.
0

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>

Comments

0

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>

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.