1

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

2
  • save $passdate in $_SESSION... Commented Jan 8, 2014 at 12:40
  • as far as i'm aware if the file is included after the variable is declared you should be fine Commented Jan 8, 2014 at 12:40

3 Answers 3

1

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.

Sign up to request clarification or add additional context in comments.

7 Comments

I think your example is just the opposite of what I'm looking for. I don't want the variables in t1 and t2 to be used in a. I want the variables in a to be used in both t1 and t2. that is if i'm reading your example correctly.
@user3125362 : i have edited my answer. I think You already have got what you were looking for.
i see what yoou are saying but i am only using one include statement. the second one is an href statement. can you show me how to use this combination please
there will be a link in a.php after clicking that will be go to t1 and there you want to get variable of a.php. that right?
the main page is a.php, it has an include(t1.php), t1 has an href connecting to t2.php. i need the variable from a.php inside t2.php
|
0

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

4 Comments

The $passdate is a derived field based on the calendar day of the year. It is created in a if statement which also contains the include("calendar.php"). If I make $passdate a global variable will it be different based on which if statement executes it? Basically, I have 31 if statements with 31 include("calender.php") statements and 31 $passdate statements. When a specific day is selected I need the calendar.php to load with that specific $passdate variable.
Hi @user3125362, I think the best thing would be to share your code via pastebin or equivalent with us.
i've never used anything like pastebin.com. i looked at it but am not sure if I paste the links to the files or if i paste the content of each file and multiple pastes. please clarify and I will share. thanks
I think it would be easiest to just paste in the code from all your files into one pastebin document, and just prefix each script's contents with the filename.
0

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

1 Comment

I tried referring to the $passdate as a global variable. The problem is that the include("calendar.php") is inside an IF statement for each day of the month. The $passdate is different inside each IF so when the include statement is ran I need that version of the $passdate to be passed.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.