Jump to content

Archived

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

georgefl

How to use variables from other included files

Recommended Posts

Hy!

I have 3 files:

 

file 1 (index.php) includes 2 files: file2.php and file3.php

 

File2.php contains $aditionalStuff and in file3.php I want to use $aditionalStuff, but it wont work (like it wasn't initialized).

 

How can I make this work?

 

index.php

include "file2.php";
include "file3.php";

 

file2.php

$aditionalStuff = 'some stuff';

 

file3.php

echo $aditionalStuff;

 

Thanks!

Share this post


Link to post
Share on other sites

Hi,

 

 

try to use your variable as a global.

Share this post


Link to post
Share on other sites

Hi,

try to use your variable as a global.

 

That makes no sense.

 

There is no trick to making variables available from within included files. If they are within scope, they are available. The example you have posted should work fine.

Share this post


Link to post
Share on other sites

Yeah, youre right, my mistake.

 

Anyway in file3.php you didnt include file2.php i think.

 

Edit: If you get index.php only its working, but if you get file3.php it has a problem

Share this post


Link to post
Share on other sites

Anyway in file3.php you didnt include file2.php i think.

 

Edit: If you get index.php only its working, but if you get file3.php it has a problem

 

I do not want to include file2 in file3. That's the problem.

And no, my example doesn't work. I have error reporting activated.

Share this post


Link to post
Share on other sites

Just post your actual code to get the quickest help with why it is not working.

 

There's several million php web sites where what you are doing works.

 

Either your include statements are failing or the few lines of code you posted out of context are either not being executed or they have a different variable scope from where the variable is set.

Share this post


Link to post
Share on other sites

×

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.