Reputation: 63
I have index.php
which has include("calendar.php")
inside it.
index.php
contains the $passdate
field which is accessible inside calendar.php
.
I also have an include("calendarnew.php")
inside calendar.php
and I want to pass the $passdate
field again. It does not get it because it actually resides inside index.php
where the calculation resides.
I hope this is not too confusing. How do I pass a derived field from one file to another to another using include statements?
I received some good answers but I don't think that I was clear. The first file is calling the second with an include statement. but, the second is calling the third using href. This causes the variable in the 1st file to not be accessable in the 3rd. I hope that clarifies things. thanks
Upvotes: 1
Views: 88
Reputation: 9351
a.php
$b = "this_from_a";
include("t1.php");
t1.php
echo "<a href=t2.php?var=".$b.">link</a>";
t2.php
echo $_GET['var'];
OUTPUT:
this_from_a
WAY2:
Use session.
Upvotes: 1
Reputation: 7156
If the code inside calendar.php and calendarnew.php is executed before $passdate is assigned, it's impossible to access it.
If you are trying to access $passdate inside a function within the calendar.php or calendarnew.php file, simply refer to it as a global varialble.
Example :
index.php file
include 'calendar.php';
//some code
$passdate = 'same value';
calendar.php file
include 'calendarnew.php';
function someFunctionName()
{
global $passdate;
return $passdate . ' append value';
}
Hope it helps
Upvotes: 0
Reputation: 1454
When you include a file in PHP, at runtime the PHP interpreter creates one large file. Where ever you have the "include()" or "require()" functions, the contents of those files are inserted there. So, in fact, you essentially end up with one single file at runtime. Thus, a variable declared at the top of the index.php file should be accessible throughout.
Having said this, please be aware of variable scope. For example, if you declare a variable at the top of the index.php file, and try to reference that variable inside a function in another file being included in the index.php file, that will not work because you are looking for a local variable in a function which is actually a global variable. To solve this problem, you use the "global" keyword to indicate that a certain variable indicator should point to the global variable.
For example:
<?php
$my_var = 1;
function my_funct() {
global $my_var;
print $my_var; // Will print 1
}
I hope that is useful.
Regards, Ralfe
Upvotes: 0