mattphilips Posted December 1, 2008 Share Posted December 1, 2008 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? Quote Link to comment Share on other sites More sharing options...
gevans Posted December 1, 2008 Share Posted December 1, 2008 <?php include 'include.php'; ?> don't need the brackets Quote Link to comment Share on other sites More sharing options...
flyhoney Posted December 1, 2008 Share Posted December 1, 2008 Are you sure the file is being included at all? Trying die('wtf')ing in the include file to make sure that its being successfully included. Quote Link to comment Share on other sites More sharing options...
MatthewJ Posted December 1, 2008 Share Posted December 1, 2008 I would agree with flyhoney... My bet is errors are turned off and you are not getting the included file. Quote Link to comment Share on other sites More sharing options...
mattphilips Posted December 1, 2008 Author Share Posted December 1, 2008 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? Quote Link to comment Share on other sites More sharing options...
gevans Posted December 1, 2008 Share Posted December 1, 2008 use the directory in the include if the file to be included is in a directory lets say 'inc' your include statement would be as follows include 'inc/include.php'; Quote Link to comment Share on other sites More sharing options...
flyhoney Posted December 1, 2008 Share Posted December 1, 2008 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'; Quote Link to comment Share on other sites More sharing options...
mattphilips Posted December 1, 2008 Author Share Posted December 1, 2008 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: " Quote Link to comment Share on other sites More sharing options...
flyhoney Posted December 1, 2008 Share Posted December 1, 2008 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. Quote Link to comment Share on other sites More sharing options...
flyhoney Posted December 1, 2008 Share Posted December 1, 2008 Also, when debugging PHP, add this to the top of your script: <?php error_reporting(E_ALL); ini_set('error_reporting', E_ALL); ?> Quote Link to comment Share on other sites More sharing options...
ScotDiddle Posted December 1, 2008 Share Posted December 1, 2008 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 Quote Link to comment Share on other sites More sharing options...
mattphilips Posted December 1, 2008 Author Share Posted December 1, 2008 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. Quote Link to comment Share on other sites More sharing options...
trq Posted December 1, 2008 Share Posted December 1, 2008 A url is NOT a path. For your variables to be brought into scope you need to include files using either a relative or absolute path, not a url. Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.