BEE Script: global [var];
BEE Tag: <beeglobal [var]>
The "global" command is valid only within a function. It declares that a variable is in the "global" Context, which means that the variable inherit the value set in the main script, and any changes to them inside the function will reflect onto the main script.
In another word, "global" variables are references to top level variables.
However, if the function calls another function, the "callee" function would not see the variables declared global in the "caller" function, unless the "callee" declares these variables "global" as well.
Example:
function welcome
{
global sitename;
var sitename = "Good Buy Shopping Mall";
var who = "{sys%auth:username}";
display "Welcome to {sitename}, {who}.<br>
";
}
var sitename = "a web site";
var who = "a user";
welcome; // Welcome to Good Buy Shopping Mall, johnc.
display "{sitename} got {who}.<br>
";
// Good Buy Shopping Mail got a user.
Please note that while variable "sitename" has been set after the function call, changes to "who" inside the function did not show up in the display.
If the variable name (var) is missing, the whole Context (all variables in the global Context) will be declared global. (Please use this carefully to avoid polluting the global environment.)
|