Define and Include problem?

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
Rax
Forum Newbie
Posts: 5
Joined: Mon Aug 25, 2008 2:18 pm

Define and Include problem?

Post 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...

Code: Select all

include check.
Is this possibly a config problem? Because this works on my other server which is running 5.2.5 also I believe.
User avatar
Ziq
Forum Contributor
Posts: 194
Joined: Mon Aug 25, 2008 12:43 am
Location: Russia, Voronezh

Re: Define and Include problem?

Post 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');
?>
 
User avatar
RobertGonzalez
Site Administrator
Posts: 14293
Joined: Tue Sep 09, 2003 6:04 pm
Location: Fremont, CA, USA

Re: Define and Include problem?

Post 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);
?>
User avatar
Bill H
DevNet Resident
Posts: 1136
Joined: Sat Jun 01, 2002 10:16 am
Location: San Diego CA
Contact:

Re: Define and Include problem?

Post 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.
Rax
Forum Newbie
Posts: 5
Joined: Mon Aug 25, 2008 2:18 pm

Re: Define and Include problem?

Post 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?
User avatar
RobertGonzalez
Site Administrator
Posts: 14293
Joined: Tue Sep 09, 2003 6:04 pm
Location: Fremont, CA, USA

Re: Define and Include problem?

Post by RobertGonzalez »

Is error reporting on and set to E_ALL?
Rax
Forum Newbie
Posts: 5
Joined: Mon Aug 25, 2008 2:18 pm

Re: Define and Include problem?

Post 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.
Rax
Forum Newbie
Posts: 5
Joined: Mon Aug 25, 2008 2:18 pm

Re: Define and Include problem?

Post 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?
User avatar
RobertGonzalez
Site Administrator
Posts: 14293
Joined: Tue Sep 09, 2003 6:04 pm
Location: Fremont, CA, USA

Re: Define and Include problem?

Post 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';
?>
Rax
Forum Newbie
Posts: 5
Joined: Mon Aug 25, 2008 2:18 pm

Re: Define and Include problem?

Post by Rax »

Thanks. Will do!
Post Reply