Click here to Skip to main content
14,430,403 members
Rate this:
Please Sign up or sign in to vote.
See more:
I am trying to access a variable $defaultPath from the php script that I have included.

However, when i am trying to echo the variable there is an error stating undefined variable. The php script is basically a nav bar (let's call it navbar.php) and this includes the helper.php which is where the variable $defaultPath is located. Moreover, the navbar.php is included in index.php in which index.php also includes helper.php. I used include_once to include the helper.php in both navbar.php and index.php but only index.php can echo the variable but not navbar.php

Here are the codes: Navbar.php

    include_once __DIR__ . '/../../helpers/helper.php';

    echo $defaultPath . "</br>";

    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <title>Interactive Digital Experience</title>
    <link rel="stylesheet" href="" integrity="sha384-ggOyR0iXCbMQv3Xipma34MD+dH/1fQ784/j6cY/iJTQUOhcWr7x9JvoRxT2MZw1T" crossorigin="anonymous">
    <link rel="stylesheet" href="/capstone-project/style.css"/>
<nav class="navbar navbar-expand-lg navbar-dark bg-dark">
    <div class="container">
        <a class="navbar-brand mb-0 h1" href=<?php echo pageUrl("index.php", $defaultPath)?>>ICT Open House</a>
        <button class="navbar-toggler" type="button" data-toggle="collapse" data-target="#navbarNav" aria-controls="navbarNav" aria-expanded="false" aria-label="Toggle navigation">
            <span class="navbar-toggler-icon"></span>
        <div class="collapse navbar-collapse justify-content-end" id="navbarNavAltMarkup">
            <div class="navbar-nav">
                <a class="nav-item nav-link" href="<?php echo pageUrl("index.php", $defaultPath) ?>">Courses<span class="sr-only">(current)</span></a>
                <a class="nav-item nav-link" href="<?php echo pageUrl("quiz.php", $defaultPath) ?>">Course Quiz</a>
                <a class="nav-item nav-link" href="<?php echo pageUrl("tour.php", $defaultPath) ?>">Tour Map</a>



$defaultPath = ($_SERVER['SERVER_NAME'] == 'localhost') ? '/capstone-project/' : '/' ;

function subview($file) {
    $file =  __DIR__ . '/../views/sub-views/'. $file;
    include_once $file;

function cssPath($file, $defaultPath) {
    $hrefPath = $defaultPath .'assets/css/'. $file;
    return $hrefPath;

function jsPath($file, $defaultPath) {
    $hrefPath = $defaultPath . 'assets/js/'. $file;
    return $hrefPath;

function pageUrl($file, $defaultPath) {
    $httpProtocol = !isset($_SERVER['HTTPS']) || $_SERVER['HTTPS'] != 'on' ? 'http' : 'https';
    $url = $httpProtocol . '://' . $_SERVER['HTTP_HOST'] . $defaultPath;
    if ($file !== 'index.php') {
        $url .= 'views/' . $file;
    return $url;


    include __DIR__.'/helpers/helper.php';

<!DOCTYPE html>
<html lang="en">
    <?php subview('header.php') ?>
    <main class="container">
            <h1>Fullscreen Landing Page</h1>
        <section class="courses">
            <div class="row justify-content-center">
                <div class="col-xs-6 col-md-4 bg-primary">Course 1</div>
                <div class="col-xs-6 col-md-4 bg-primary">Course 2</div>
                <div class="col-xs-6 col-md-4 bg-primary">Course 3</div>
                <div class="col-xs-6 col-md-4 bg-primary">Course 4</div>
                <div class="col-xs-6 col-md-4 bg-primary">Course 5</div>


    <?php subview('footer.php') ?>

Note: I am not trying to access the variable inside a function scope.

What I have tried:

I tried changing it back to include but error occurs because of duplicate functions.

I also tried echo-ing the the $defaultPath variable in index.php and there are no undefined error but when i try to do it in navbar.php it gives me undefined sadly
Richard MacCutchan 2-Nov-19 4:54am
I am not a PHP expert, but are you sure the included file needs the <?php and ?> tags around it?
Richard Deeming 5-Nov-19 10:28am
Are you sure that the included file is actually being found? You could try using require_once instead of include_once, so that you get an error if the file can't be found.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100