Jump to content

Is there any reason why my php includes won't read my variables?


mattphilips

Recommended Posts

Is there any reason why this code wouldn't work?

 

### index.php ###
<?php 
$x = 123;
include('include.php');
?>

### include.php ###
<?php 
$y = $x;
echo $y;
?>

 

If I do an isset check it comes back false. Could it be a php.ini setting? Or am I doing something wrong?

Ok uh...

 

I am getting the included file. But one thing I noticed is that the variables only work if the included file is in the same directory as the index file.

 

I'm guessing that's the way it's supposed to work. Is  there a way around this?

When you include a file, you are specifying that path to that file.

 

So if 'include.php' is in the same directory as 'index.php':

include 'include.php';

 

If 'include.php' is in a directory called 'roflcopters', then:

include 'roflcopters/include.php';

I am doing the full path.

 

include('http://www.domain.com/files/includes/include.php');

 

What I just noticed after I posted this is that it includes the file either way. But ther variables only seem to carry over if the include is in the same or a sub directory of the index file.

 

For example:

 

With the include file in the same directory as the index file:

echo 'test: ' . $x;

 

returns: "test: 123"

 

If the include file is outside the directory of the index the same code returns:

 

"test: "

 

I don't think that is correct behavior.  Including a file, no matter where it is, should bring all variables in that file into scope. 

 

This, however:

include('http://www.domain.com/files/includes/include.php');

 

Is not exactly best practice.  If the files are on the same server, there is no need to specify the path to the include file with a url.

mattphilips ,

 

    Also, the home (dir) of any file you want to include must be specified in the php.ini  " include_path " directive.

 

I suspect that if you coded error_reporting['E_ALL'];  at the beginning of your script, it will tell you that the file can't be found.

 

My ini include_path statement looks like this :

 



include_path=".:/usr/local/Zend/Core/share/pear:/usr/local/Zend/www/dev:/usr/local/Zend/www/dev/include:/usr/local/Zend/www/dev/font:/usr/local/Zend/www/dev/include/lang:/usr/local/Zend/www/dev/classes:/usr/local/Zend/www/dev/javascript:/usr/local/Zend/www/dev/library:/usr/local/Zend/www/dev/WebSVN:/usr/local/Zend/www/dev/WebSVN/languages:/usr/local/Zend/www/dev/smarty:/usr/local/Zend/www/dev/smarty/libs:/usr/local/Zend/www/dev/smarty/templates:/usr/local/Zend/www/dev/smarty/template_c:"

 

Scot L. Diddle, Richmond VA

using the path ../include.php worked.

 

However that only works for some of my files that I want to use the same include with.

 

Also... Putting the

 

<?php
error_reporting(E_ALL);
ini_set('error_reporting', E_ALL);
?>

 

Didn't return any errors. It either works as intended or it includes the files but the variables are not set.

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.