Page 1 of 1
Define and Include problem?
Posted: Mon Aug 25, 2008 2:38 pm
by Rax
Hi, this is really confusing me. I have config.php that is loaded with the include function into check.php. For some reason though, check.php acts as if the variables don't exist...
config.php
Code: Select all
<?php
$blue='hello';
define('red', 'bye');
?>
check.php
Code: Select all
<?php
include_once("config.php");
echo $blue;
echo red;
echo "include check.";
?>
When check.php is called the result is just...
Is this possibly a config problem? Because this works on my other server which is running 5.2.5 also I believe.
Re: Define and Include problem?
Posted: Mon Aug 25, 2008 2:45 pm
by Ziq
Are you sure that config.php included?
replace config.php
Code: Select all
<?php
echo 'config.php - OK!';
$blue='hello';
define('red', 'bye');
?>
Re: Define and Include problem?
Posted: Mon Aug 25, 2008 2:48 pm
by RobertGonzalez
Instead of include_once() try this:
Code: Select all
<?php
require_once 'config.php';
// ... yadda yadda
?>
And prior to including it, make sure your error reporting is cranked up so you can see any errors this is throwing.
Code: Select all
<?php
ini_set('error_reporting', E_ALL);
ini_set('display_errors', 1);
?>
Re: Define and Include problem?
Posted: Mon Aug 25, 2008 2:49 pm
by Bill H
Depending on how your error reporting is set, it may not be finding the file to include. Where is the file located, and what is the setting for your "include path"?
Edit: stepping on Everah's post is what happens when one is a slow typist.
Re: Define and Include problem?
Posted: Mon Aug 25, 2008 3:30 pm
by Rax
Hi, thanks for your replies. It actually turns out my problem is more complex. The script I showed you actually wasn't working because of another error which is fixed now. Now, my problem is this.
I currently have an index.php which includes check.php which includes config.php. When I call check.php the variables are found. When I call index.php though, I get...
Notice: Use of undefined constant BLUE in check.php
Any ideas why the variables get lost?
Re: Define and Include problem?
Posted: Mon Aug 25, 2008 3:40 pm
by RobertGonzalez
Is error reporting on and set to E_ALL?
Re: Define and Include problem?
Posted: Mon Aug 25, 2008 3:49 pm
by Rax
Yes it is. All it gives me when I call index.php is...
Code: Select all
Notice: Use of undefined constant BLUE - assumed 'BLUE' in C:\path\check.php on line 84
My code isn't exactly this but basically...
Code: Select all
<?php
//config.php
echo "config";
define('BLUE', 'hello');
?>
Code: Select all
<?php
//check.php
include_once('config.php');
?>
Code: Select all
<?php
include_once('check.php');
echo BLUE;
?>
The output is what I stated above. The "check" text doesn't show either.
Re: Define and Include problem?
Posted: Mon Aug 25, 2008 3:59 pm
by Rax
Oh ok! I isolated the problem. But I don't know how to fix it. I should of mentioned check and config are in a separate folder called includes. So it looks more like...
Code: Select all
<?php
//includes/config.php
echo "config";
define('BLUE', 'hello');
?>
Code: Select all
<?php
//includes/check.php
include_once('config.php');
?>
Code: Select all
<?php
//index.php
include_once('includes/check.php');
echo BLUE;
?>
The code in the post before this does actually work sorry. But when the files are put in a separate folder for some reason it doesn't work. Any ideas?
Re: Define and Include problem?
Posted: Mon Aug 25, 2008 4:24 pm
by RobertGonzalez
Pathing is why it doesn't work. That is a common mistake when it comes to including. You might want to consider creating a path_to_root variable or something along those lines so that whenever anything is included you can just say
Code: Select all
<?php
include_once $basepath . 'includes/includefiles.php';
?>
Re: Define and Include problem?
Posted: Mon Aug 25, 2008 4:29 pm
by Rax
Thanks. Will do!