We launched new forums in March 2019—join us there. In a hurry for help with your website? Get Help Now!
    • 33101
    • 46 Posts
    If I put the following code in a regular PHP script run on the server - it works, as a snippet it doesn’t

    [tt]$test="worked!";

    function testprint()
    {
    print "test: ".$GLOBALS[’test’]."
    ";
    }

    testprint();[/tt]

    Can someone explain what’s going on? Thanks -
      • 11975
      • 2,542 Posts
      $test="worked!";

      function testprint($test)
      {
      print "test: ".$test."
      ";
      }

      testprint();

      /***** OR ****/

      $test="worked!";

      function testprint($test)
      {
      global $test;
      print "test: ".$GLOBALS[’test’]."
      ";
      }


      :-)
      testprint();
        Made with MODx : [url=http://www.copadel.com]copadel, fruits et l
        • 18397
        • 3,250 Posts
        You should not use print/echo in a snippet, you need to return the value.
          • 28042 ☆ A M B ☆
          • 24,524 Posts
          $test="worked!";
          
          function testprint()
          {
            //print "test: ".$GLOBALS['test']." <br>";
            return "test: ".$GLOBALS['test']." <br />";
          }
          
          $output = testprint();
          
          return $output;
            Studying MODX in the desert - http://sottwell.com
            Tips and Tricks from the MODX Forums and Slack Channels - http://modxcookbook.com
            Join the Slack Community - http://modx.org
            • 33101
            • 46 Posts
            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?
              • 18397
              • 3,250 Posts
              Using a return method ensures that the output you intend to send back to the browser ends up in the correct location. Using print or echo can sometimes lead to code being in odd places (although most of the time it works fine)