Jump to content

Multiple includes losing variables


Johnblythe

Recommended Posts

Hey all,

 

just starting out w php and ran into a problem pretty quickly. I'm

including several files into each page. The variables in the first include file are functional in the main page, but are undefined in subsequent include files.

 

I've tried using define() to make them global variables but still have had no luck.

 

Any help would be greatly appreciated!!

Link to comment
Share on other sites

thanks for the quick response!

 

i can include the code if you'd like, to what extent would be helpful? don't want to throw it all on here and it be more to wade through than necessary.

 

also, the include files themselves work fine, at least to the extent of being included properly and displaying what it needs to. the problem comes in when i try to use, within those included files, variables that were defined in another file.

 

here's the basic setup. i have a meta.php file that has some vars declared to be used throughout the site (for links and such). each page (about.php, contact.php, etc) pulls this meta.php include in at the beginning of the page code. later on, in the footer.php include page, when i try to access variables defined in meta.php (so an include file is trying to get data that has been setup in a previously included file), the vars are null.

 

does that help refine the situation better? thanks!

Link to comment
Share on other sites

Your code is either including (or not successfully including) the file containing the settings or including the file using those settings in such a way that the normal variable scope is not being maintained or your code is clearing variables so that they are not available later in the code.

 

Due to its' general purpose nature, there is not a 'one symptom' is always caused by 'one specific thing' relationship in programming. 1013 different people could have written your posts in this thread and each of them could have a different error in their programs that is causing a variable to have a value at one point but not at another point.

 

What specifically didn't you understand about the reply you got -

Best guess is you are including files using URLs instead of file system paths.

 

It would take seeing your code to be able to help you with what it is or is not doing.

That contained a best guess cause (which you did not bother to provide any information to confirm or eliminate as a cause)  because you supplied zero relevant information in your first post and your second post did not supply any relevant information either about what your code is doing that could be causing the symptom.

Link to comment
Share on other sites

excuse me? i understood perfectly fine what you said about "best guess is..." not sure if that was intentionally rude or not, but either way unnecessary, as was the comment

That contained a best guess cause (which you did not bother to provide any information to confirm or eliminate as a cause)  because you supplied zero relevant information in your first post and your second post did not supply any relevant information either about what your code is doing that could be causing the symptom.

 

on the contrary, i asked for further clarification as to what i should include in my more detailed post, which you didn't bother responding to.

i can include the code if you'd like, to what extent would be helpful?

 

if you'd like to help, great. if not, fine. let's just not get into a pissing match here, okay? i'm trying to provide whatever relevant info i can. i realize there can be nth reasons as to why this is malfunctioning. i program in .net, javascript and flash daily, so programming and the logic behind it isn't what's new for me. php, on the other hand, is brand new for me. i've read articles on variables, global variables, constants and the likes which is why i'm so stumped at this point; the things i've read i've tried to employ and yet i've got nothing but more/other errors.

 

here's a sample page code:

<?php
require('includes/vars.php');
require($url."includes/meta.php");
include($url.'includes/dropper.php');
include($url.'includes/header.php');
$name = About Us;
?>

<div id="content_wrapper">
	<div id="head" class="<?php echo($name);?>"></div>	

	<div id="content">
  		
  		<?php include($url.'includes/sides/something_else.php');?>
  			        
        <div id="main">
                ................
<?php include($url.'includes/footer.php');?>
<?php include($url.'includes/foot.php'); ?>

 

in the foot.php, the following isn't working:

<a href="<? echo($home_url); ?>">Home</a> |

 

so somewhere after including the vars.php file in the top of the page and thus the var $url working in the page and then including subsequent portions (such as foot.php) that call the variable also, the variable is dumped, lost in transition, something.

 

so either i'm missing something key in the way PHP works w/ scoping of variables or including of files, or something syntaxtually is amiss.

 

thanks again for any help w/ this. have a great christmas-

jb

 

 

 

Link to comment
Share on other sites

hey guys,

 

thanks for the input.

 

teamatomic: the vars that i'm trying to play w/ here are for the purpose of defining the location of various pages and subpages so that when we go to make changes in the future, changing a pages area from one location on the site to another, we will only have to change the location in the vars.php include file instead of on each individual page. is there a better way to go about this then how i am? also, that being said, there are no functions going on at this point, just calling vars for navigational purposes.

 

raj: i have no clue whatsoever as to what you're referring to :). please pardon my ignorance, could you explain further?

 

also, i have no idea what is going on with this simple code:

define('ABSPATH', $_SERVER['DOCUMENT_ROOT']."/");

i've run a little if statement to echo back whether or not it is defined. it comes back as true, so the line is working fine, but when i try to call it it doesn't work unless i call it with constant("ABSPATH"). from the documentation i've read online, i should be able to call it straight up like a variable would be called, right? or totally wrong?

 

thanks again guys. hope everyone has had a great christmas-

 

jb

 

 

Link to comment
Share on other sites

ahhh gotcha. okay, i'll check into that. i had used the standard <?php deal initially but then saw in lots of code examples that it was shortcutted so i started doing that. i don't think it was working previously when i used <?php but i'll go back and double check everything to make certain.

 

thanks!

Link to comment
Share on other sites

okay, so i've tried checking into some of the above things and i'm still stumped. i have no clue why this is being such a pain or so difficult since it seems like a very simple task.

 

new question first: is it bad practice or stupid to even try to do what i'm doing? namely, to have a vars.php page included within every page that contains different variables for each page and include file? for instance $home_url, $about_url, etc. and $dropper_inc, $foot_inc, etc.? the reason for doing so was, in my mind, to try to avoid having to go through each page when directories change, expansion of the site is done, and other changes like those.

 

i've read quite a bit about vars and includes and have not found any reason as to why the variables are lost/emptied after the file including the include file (that houses the variables) uses them.

 

thanks for any further assistance-

Link to comment
Share on other sites

Since you are apparently using a URL for the include (it would help is you showed what $url actually is), none of the variables from the main code are availabe in the included file(s).

 

Using a URL (i.e. http://somedomain.com/somefile.php) causes your web server to make a HTTP request back to itself, the same as if you browsed to the file. The file being included is processed in a completely separate process on the server and it is in its' own SCOPE.

 

You must use a file system path to include files in order to maintain variable scope.

Link to comment
Share on other sites

alrighty guys, here's so example code. hopefully this will help explain what i've not done the best job of conveying in previous posts (my apologies for that).

<?php 
/*a main page, includes a few files */
include '../includes/meta.php'; //this works, this file has an include for a vars.php file that is setup w/ all the vars needed throughout the site
include '$header_inc'; //this works, the var is defined in the vars.php file included in the meta.php file above
?>

<div id="wrapper">
....
</div>

<?php 
include '$foot_inc';  //this works, also a var setup in vars.php
?>

<?php
//vars.php file example
$url = "http://example.com/";
$about_url = $url.'about/about.php;'
$mission_url = $url.'about/mission.php';
...
?>

<!--foot.php file example-->
<ul class='nav'>
<li><a href="<?php echo '$url';?>">Home</a></li> <!--these vars are all empty-->
<li><a href="<?php echo '$about_url'?>">About</a></li>
<li><a href="<?php echo '$mision_url'?>">Mission</a></li>
...
</ul>

 

thanks!

Link to comment
Share on other sites

Dont wrap them in single quotes either change them to " or remove them all together..

<ul class='nav'>
<li><a href="<?php echo $url; ?>">Home</a></li> <!--these vars are all empty-->
<li><a href="<?php echo $about_url; ?>">About</a></li>
<li><a href="<?php echo $mision_url; ?>">Mission</a></li>

Link to comment
Share on other sites

no luck w/ the double quote or no quotes. i've read here and elsewhere about php using the system directory instead of typical url structures, but i don't udnerstand that process well enough to figure out if it's the problem. if so, it'd appear that i need to use a var or constant that gets the doc root. so $url would be something like this:

<?php $url = $_SERVER['DOCUMENT_ROOT'];?>

 

right?

 

but my hang up w/ that (or understanding when/why to use it or not) is that the variables work in the file that includes them (meta.php), the file that includes that file (examplepage.php), but not any of the other files included in that page (example.php including foot.php or nav.php, etc).

 

i don't get why it works some of the time but not all of the time and how using the DOCUMENT_ROOT would help that. sorry fo rbeing such a php n00b here everyone, thanks for your patience and time-

Link to comment
Share on other sites

hope this code helps you guys help me, sorry for not relaying my problems all that well previously. thanks for bearing w/ me being a php n00b!

 

<?php
//example meta.php
$url = "http://example.com/";
include('../includes/vars.php');
?>

<!--begin main page-->
<?php
//example main page
include("../includes/meta.php"); //works
include($header_inc); //works, this is a variable defined within the vars.php file that the meta.php file includes
include($dropper_inc); //works, also a var from var.php
?>
<div id="wrapper">
......
</div>
<?php
include($foot_inc);//works, var from vars.php
?>
<!--end main page-->

<!--begin foot.php-->
<!--none of the below vars from the vars.php file are populated w/ data, all empty-->
<ul>
<li><a href="<?php echo($url);?>">Home</a></li>
    <li><a href="<?php echo($about_url);?>">About</a></li>
    ....
    <li><a href="<?php echo($contact_url);?>">Contact</a></li>
</ul>
<!--end foot.php-->

 

again, my biggest confusion is that the vars work for the file that includes the vars.php file, and for the file that includes the meta.php, but then stop after any other include files try to use them. this is sooooooooooo frustrating bc it seems so elementary. thanks!

Link to comment
Share on other sites

so i've got things working now via a little work around, simply including the vars.php file into each of those include files that were losing the variables due to scope or whatever issue it was.

 

however, i'd still like to understand the why of it all. i don't follow why it is that the main page pulling in all these files would lose the variables half way through the process.

 

so, though i don't technically need a 'fix' like i did, i'd still love to have an answer so as to better understand what php is doing in all of this. thanks!

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.