Jump to content

[SOLVED] Variables in Functions


UnsuitableBadger

Recommended Posts

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

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

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.