We launched new forums in March 2019—join us there. In a hurry for help with your website? Get Help Now!
    • 13306
    • 19 Posts
    Hi there

    I'm using Evolution on a nice site that's been live for a year or two. I'm adding a snippet for some custom PHP code. I need to access a variable that's outside of a function within the snippet. On a standalone page, I'd use the global keyword. That doesn't seem to work within the snippet.

    For example, is there a way to do the equivalent to this?:

    <?php
    $a = 1;
    function Sum()
    {
    global $a;
    /(/$a would be 1)

    }
    ?>

    Thanks hugely in advance.

    This must have been asked a hundred times.

    I have to say, MODX is excellent - both Evolution and Revolution. I recommend it to everyone and wouldn't use anything else.

    This new website, and especially the forum design, is awful! I know that's not the best way to get help! - a criticism. I need to defend why I can't find an answer. I have tried to search. I'd not logged in since the change - and this design is so bad, the forums literally aren't usable for their proper purpose.

    I hope there are plans to redesign this site or to revert to something that works. It's an awful shame, and such a waste of so much valuable info.
      • 28042 ☆ A M B ☆
      • 24,524 Posts
      https://forums.modx.com/thread/10358/resolved-codding-in-a-snippet#dis-post-59592

      This came up in a search on the term "global" in the forum search field.
        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
        • 3749
        • 24,544 Posts
        PHP in MODX snippets and plugins is regular PHP, so what you're doing should work.

        That said, I've run into situations where the global variable was not recognized inside the function. I never figured out why, but the solution is to pass the variable to the function as an argument.
          Did I help you? Buy me a beer
          Get my Book: MODX:The Official Guide
          MODX info for everyone: http://bobsguides.com/modx.html
          My MODX Extras
          Bob's Guides is now hosted at A2 MODX Hosting
          • 13306
          • 19 Posts
          Quote from: sottwell at Jan 09, 2012, 12:31 AM
          https://forums.modx.com/thread/10358/resolved-codding-in-a-snippet#dis-post-59592

          This came up in a search on the term "global" in the forum search field.
          That's interesting. Declaring global outside of the function. Thanks for that. I'll test.

          I've found $GLOBALS['myvar'] = also works.


          *A note on the problems with this forum design, to explain what I said above - in case it helps.

          The popup menus cover the crumb trail - never do that. All forum titles are greyed out except on hover - it looks like they're disabled. The search box is out of position as if it's site-wide search. Individual forums aren't selectable, i.e., General, Revolution and so on. A second click on quote seems to replace a reply. And finally, jQuery flyouts and similar are/were very fashionable, but come on - don't go mad with it on a website for developers.

          I'd suggest a good look at phpBB or SMF. I hope that's useful feedback and not an insult to anyone. [ed. note: j4j7 last edited this post 14 years, 9 months ago.]
            • 28042 ☆ A M B ☆
            • 24,524 Posts
            Keep in mind that in Evo, snippets are run via the PHP eval() function, so some things are not quite as might be expected.
              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
              • 13306
              • 19 Posts
              I will. Thanks again.