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.
$_SESSIONwithsession_start(). Maybe it wasn't included in the code for display purposes, but that would give you empty variables.$nav1and$nav2defined withinunderlineActivePage()doesn't know about the$nav1and$nav2within theindex.phppage.