Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

PHP

Surendra Kulkarni
Surendra Kulkarni
6,826 Points

config.php file is not recognized by header.php

If i use

<?php
echo BASE_URL;
?>

in header.php with index.php having

<?php
include( ROOT_PATH. "/inc/header.php");
?>

Warning "unrecognised variable BASE_URL" is displayed.

But if I enter

<link rel="stylesheet" href="<?php echo "BASE_URL" ; ?>/css/style.css" type="text/css">

It works. I have tried 1.including config.php in header.php, 2. Chnaging "" to ''. but it still does not accept constant in header.php.

I will grateful for any help. Cheers!

3 Answers

Have you defined BASE_URL?

If you have, where is it being included and what does that file look like?

Surendra Kulkarni
Surendra Kulkarni
6,826 Points

Hi Tom Yes! In the config.file. If I include config.file in the header.php. BASE_URL constant is recognised so in a way the proble is solved. But the issue is even though config.php is included before header.php in receipt/index.php BASE_URL is not in scope of header.php

Thanks for your help & a Happy New Year!

Cool. It should be available in header.php if defined beforehand.

This might be a bit overkill, but I would check the value of BASE_URL all the way from where you define it to where you use it. By chopping up your script ("binary chopping") you should be able to narrow down where the break is. If everything is defined as you say it is, you shouldn't have this issue - so my inclination is to suggest you have a bug in your code.

<?php

echo "before include";
echo BASE_URL;
exit;

Then:

<?php

echo "before include";
echo BASE_URL;
echo "<br>";

// Include some files

echo "<br>";
echo "after include";
echo BASE_URL;
echo "<br>";
exit;

Try this all over the place and see if the results are as expected. Also show us your config.php file

You also :-D !

Surendra Kulkarni
Surendra Kulkarni
6,826 Points

Hi Tom thanks here is the code:

<?php
define("BASE_URL", "/shirts4mike_local/");
define("ROOT_PATH", $_SERVER["DOCUMENT_ROOT"]  . "/shirts4mike_local/") ;

receipts/index.php

<?php
require_once("../inc/config.php");
$pageTitle = "Thank you for your order!";
$section = "none";
include( ROOT_PATH. "/inc/header.php"); ?>

    <div class="section page">

        <div class="wrapper">

I have tested the code with echoing our BASE_URL before but which gives the same message unknown variable.

I agrr with you that there is a bug in the code some were. I will give it a go again with echoing and let you know the result.

Cheers!