We launched new forums in March 2019—join us there. In a hurry for help with your website? Get Help Now!
    • 44881
    • 3 Posts
    Hi there,
    I have a problem.
    For example, I created simple snippet "Test":



    Who knows why in the snippet call, at the end of "Hello World" appears "1"?



    This is a problem in the server or engine?

    Thanks in advance for your help.

    This question has been answered by m.engel. See the first response.

    [ed. note: dr_dru last edited this post 12 years, 2 months ago.]
    • discuss.answer
      • 40706
      • 128 Posts
      I think MODX 2.3.1 fix this.
      But the main Problem is that snippets should have to return values, not just echo it. And if you just echo there should an return ''; in the end.

      <?php
      return "Hello world!";

      or
      <?php
      echo "Hello world!";
      return '';

      does exactly what you want, even in 2.3.0
        • 44881
        • 3 Posts

        I recently started to learn php

        thank you smiley
          • 28042 ☆ A M B ☆
          • 24,524 Posts
          You can just use return; it doesn't need to be return '';
            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
            I prefer return ''; as a matter of personal preference.

            BTW, you generally should not use echo or print in a MODX snippet. Instead, do this:

            <?php
            $output = "Hello world!";
            return $output;


            If you need to return more than one line, do this:

            $output = '<p>Line one</p>';
            $output .= '<p>Line two</p>';
            $output .= '<p>Line three</p>';
            
            return $output;


            It's also good form to "initialize" the variable at the top of the snippet:

            $output = '';


            Here's an example showing why you should do that:

            <?php
            $docSize = $modx->getOption('docSize', $scriptProperties, 0);
            
            if ($docSize > 50) {
               $output = 'Large';
            } elseif ($docSize < 50) {
               $output = 'Small';
            }
            
            return $output;
            


            If docSize is exactly 50, neither of the statements that set $output will execute and the last line will throw an "Undefined Variable $output" PHP warning if E_NOTICE errors are on because $output is never set to a value.

            Similarly all array variables should be initialized with something like this:

            $myArray = array();



              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