UnsuitableBadger Posted September 9, 2009 Share Posted September 9, 2009 Hi everyone I've got a main entry point file (index.php) which looks as follows: require('variables.php'); require('functions'); require('tasks'); myFunction(); in the variables.php file I have: $myVariable = 1; in the functions.php file I have: function myFunction() { echo $myVariable; } in the tasks.php file I have: echo $myVariable; My question is how come the tasks.php file will echo out the content of $myVariable but when calling myFunction() from the index.php it doesn't recognise the variable. Do functions just ignore any variables that are not passed to them or that are not global variables. Any ideas of how to make this work? Link to comment https://forums.phpfreaks.com/topic/173631-solved-variables-in-functions/ Share on other sites More sharing options...
Garethp Posted September 9, 2009 Share Posted September 9, 2009 http://au.php.net/manual/en/language.variables.scope.php Link to comment https://forums.phpfreaks.com/topic/173631-solved-variables-in-functions/#findComment-915254 Share on other sites More sharing options...
UnsuitableBadger Posted September 9, 2009 Author Share Posted September 9, 2009 Heh wasn't quite googling the right thing, helps when you know what to search for. Thanks so much, chocolate is in the mail Link to comment https://forums.phpfreaks.com/topic/173631-solved-variables-in-functions/#findComment-915258 Share on other sites More sharing options...
Garethp Posted September 9, 2009 Share Posted September 9, 2009 No problem. I just like to help those who are starting off. I remember the first time I encountered a variable scope problem. I was learning VB, and wanted to globalize a variable, but I had no idea what to do, and neither did my teacher (He was useless). So because I had no idea what to google, I reverted to using a caption, hiding it, and storing my variable data in it. Good times. Anyway, good luck Link to comment https://forums.phpfreaks.com/topic/173631-solved-variables-in-functions/#findComment-915260 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.