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? 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. 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!'; ?> 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; ?> 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. 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 Link to comment https://forums.phpfreaks.com/topic/138749-require_once/#findComment-725524 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.