Jump to content

Variable Spanning Modules


ameliabob
 Share

Recommended Posts

I have several modules and am trying to pass a variable between them

one.php<?phpglobal $switch;function SetSwitch(){global $switch;     $switch = true;}?>in two.php<?phpfunction TestSwitch(){global $switch    echo( "switch is ".$switch );}

But the $switch doesn't get carried forward to two.phpWhat am I missing?

Link to comment
Share on other sites

"global" means global within a single script. An external script can be made "part" of a script using an include or require statement. Then the global variables will be available to both scripts. But if you are running the scripts at separate times, global variables from one will not be available to the other. To get that functionality, without writing data to a file or database, you'll need to use sessions.

Link to comment
Share on other sites

"global" means global within a single script. An external script can be made "part" of a script using an include or require statement. Then the global variables will be available to both scripts. But if you are running the scripts at separate times, global variables from one will not be available to the other. To get that functionality, without writing data to a file or database, you'll need to use sessions.
So if I had an include of one in the othertwo.php<?phpinclude ("one.php");etc.?>That would allow passing the parameter back and forth?How about three.php<?phpinclude ("one.php");include ("two.php");?>Would this also allow the communications back and forth between the three of them?
Link to comment
Share on other sites

To use a variable from within a file, that file needs to include the rest. In your examples, one.php can manupulate only its variables, two.php can manipulate the variables from one.php and itself, and three.php can manipulate the variables from one.php, two.php and itself. This isn't exactly "back and forth", because one.php can't manipulate variables from two.php or three.php without including them.If you want to share all variables between all of those files without worrying for cyclic references (A incudes B, then B includes A, then A includes B again....), you can use include_once instead of include. It will only include a file if it's not included already (A includes B, B tries to includes A, but it's included, so it doesn't do so).

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

×
×
  • Create New...