georgefl Posted August 26, 2010 Share Posted August 26, 2010 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! Quote Link to comment https://forums.phpfreaks.com/topic/211790-how-to-use-variables-from-other-included-files/ Share on other sites More sharing options...
bh Posted August 26, 2010 Share Posted August 26, 2010 Hi, try to use your variable as a global. Quote Link to comment https://forums.phpfreaks.com/topic/211790-how-to-use-variables-from-other-included-files/#findComment-1103957 Share on other sites More sharing options...
trq Posted August 26, 2010 Share Posted August 26, 2010 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. Quote Link to comment https://forums.phpfreaks.com/topic/211790-how-to-use-variables-from-other-included-files/#findComment-1103968 Share on other sites More sharing options...
bh Posted August 26, 2010 Share Posted August 26, 2010 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 Quote Link to comment https://forums.phpfreaks.com/topic/211790-how-to-use-variables-from-other-included-files/#findComment-1103974 Share on other sites More sharing options...
georgefl Posted August 26, 2010 Author Share Posted August 26, 2010 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. Quote Link to comment https://forums.phpfreaks.com/topic/211790-how-to-use-variables-from-other-included-files/#findComment-1103981 Share on other sites More sharing options...
PFMaBiSmAd Posted August 26, 2010 Share Posted August 26, 2010 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. Quote Link to comment https://forums.phpfreaks.com/topic/211790-how-to-use-variables-from-other-included-files/#findComment-1103983 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.