jogurt1 Posted December 29, 2008 Share Posted December 29, 2008 Hi Guys, I have a quick question concerning 'require_once'. Lets say I have 2 files, file 1 and file 2. In file 1, I have a 'require_once(file 2)'. Does that mean that file 2 can access variables in file 1? Quote Link to comment https://forums.phpfreaks.com/topic/138749-require_once/ Share on other sites More sharing options...
flyhoney Posted December 29, 2008 Share Posted December 29, 2008 Yes, as long as require_once() is called before the variables are referenced. Quote Link to comment https://forums.phpfreaks.com/topic/138749-require_once/#findComment-725440 Share on other sites More sharing options...
flyhoney Posted December 29, 2008 Share Posted December 29, 2008 1.php <?php echo $foo; // $foo is not defined require_once '2.php'; echo $foo; // prints "pterodactyl!" ?> 2.php <?php $foo = 'pterodactyl!'; ?> Quote Link to comment https://forums.phpfreaks.com/topic/138749-require_once/#findComment-725444 Share on other sites More sharing options...
jogurt1 Posted December 29, 2008 Author Share Posted December 29, 2008 Ok great thanks! So the following should work then: 1.php <?php echo $foo = "lol"; require_once '2.php'; //prints "lol" ?> 2.php <?php echo $foo; ?> Quote Link to comment https://forums.phpfreaks.com/topic/138749-require_once/#findComment-725518 Share on other sites More sharing options...
premiso Posted December 29, 2008 Share Posted December 29, 2008 <?php $foo = "lol"; require_once '2.php'; //prints "lol" ?> That is how you should do it. Quote Link to comment https://forums.phpfreaks.com/topic/138749-require_once/#findComment-725520 Share on other sites More sharing options...
jogurt1 Posted December 29, 2008 Author Share Posted December 29, 2008 oops yes thanks! problem solved Quote Link to comment https://forums.phpfreaks.com/topic/138749-require_once/#findComment-725524 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.