I don’t know if the posts about echo were meant as proper coding practices for MODx (which is good to know), but I tested the following code on windows and linux machines and it works.
echo "this is a test <br>";
$globalOne="globalOne";
global $globalTwo;
$globalTwo="globalTwo";
function testglobal() {
global $globalOne, $globalTwo;
echo "globalone: $globalOne<br>";
echo "globaltwo: $globalTwo<br>";
}
testglobal();
outputs (inside the template for this page):
this is a test
globalone:
globaltwo: globalTwo
I think the answer to my question is that a snippet is not running in the global context. Is this correct? It seems that to make a variable global from the snippet to a function you have to use global in the snippet which puts it into the global context and then global in the function works.
As for the recommendation of using only return, what is the reason for this?