pippo_jedi Posted August 24, 2007 Share Posted August 24, 2007 Hello, I have a problem with some code i'm writing and I realized I lack knowledge about how to properly handle variables... I have a file foo.php that has the variables $foo_1 and $foo_2 now I have a main.php in which there is a function bar which includes foo.php How can I make $foo_1 and $foo_2 availlable to the rest of the script in main without resorting to returning thngs? I bet it is a very silly question bye bye Link to comment https://forums.phpfreaks.com/topic/66550-question-about-variable-handling/ Share on other sites More sharing options...
Caesar Posted August 24, 2007 Share Posted August 24, 2007 If you want to use those variables in a function in 'main.php', you have to make them available to the function by... <?php include'foo.php'; function cancer() { global $foo_1; //Now we can use this variable in the cancer() function in 'main.php' $message = "Hello $foo_1"; return $message; } ?> Link to comment https://forums.phpfreaks.com/topic/66550-question-about-variable-handling/#findComment-333356 Share on other sites More sharing options...
pippo_jedi Posted August 24, 2007 Author Share Posted August 24, 2007 thank you for hte answer but my problem is the opposite foo.php <?php $foo_1="I'm foo 1"; $foo_2="I'm foo 2"; ?> main.php <?php function bar(){ include (foo.php); } echo $foo_1; //this doesn't work I don't want to use return($foo_1); ?> thank you for you help Link to comment https://forums.phpfreaks.com/topic/66550-question-about-variable-handling/#findComment-333361 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.