We launched new forums in March 2019—join us there. In a hurry for help with your website? Get Help Now!
    • 28042 ☆ A M B ☆
    • 24,524 Posts
    I’ve got a snippet to grab a value from a cookie. This value is used in a number of places; to include a language file and to set the startId for Wayfinder calls among others.
    <?php
    if(isset($_COOKIE['Language'])) {
        $lang=$_COOKIE['Language'];
    } else if($modx->documentObject['parent'] == 0) {
        $lang=$modx->documentIdentifier;
    } else {
        $lang=$modx->config['site_start'];
    }
    // echo $lang;
    // return '';
    return $lang;
    ?>

    Which would be better to use, return $lang; or echo $lang; 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
      • 7231
      • 4,205 Posts
      May be a case of the blind leading the blind here, but...

      I have a similar snippet and I use the simple return $var; method. Is it better? It seems that it would be since it is less code to execute and the return will be executed anyway in either option.

      If called from within a function, the return() statement immediately ends execution of the current function, and returns its argument as the value of the function call.
        [font=Verdana]Shane Sponagle | [wiki] Snippet Call Anatomy | MODx Developer Blog | [nettuts] Working With a Content Management Framework: MODx

        Something is happening here, but you don&#39;t know what it is.
        Do you, Mr. Jones? - [bob dylan]
        • 3749
        • 24,544 Posts
        I’m as blind as the next guy here, but I think echo might put a tiny bit less strain on the server.
        i think the return statement will put the information on the stack and the snippet parser will pull it off and then echo it. If you echo it directly, it should cut out two of those three steps. The difference is probably trivial, however, unless $lang is very long.

        My general preference is to return stuff rather than echo it because it makes it easier to modify the code in the future if you decide to include any extra output. It also helps keep the habit of having a single exit point for the snippet which makes it infinitely easier to debug and maintain.

        Bob
          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
          • 27376
          • 576 Posts
          Snippets are run inside an ouput buffer that concatenates both the return value and whatever is echo’d during execution (look at the [tt]evalSnippet()[/tt] function in [tt]manager/includes/document.parser.class.inc.php[/tt]).

          In my practice I always return the output of my snippet. I have no strong reasons for doing it this way, but I’ve built a little "toolkit" to ease creation of complex snippets (those with language strings, presets, templates, etc.), and it is built around snippets returning their result rather than echoing it.

          I guess in my mind the main reason for returning a value is this: 1) In an object-oriented environment, you should limit the areas where information is echo’d to designated output controllers; in this case, the DocumentParser object. Echoing values is a fantastic debugging tool but having it littered throughout your source just seems sloppy to me.

          Just my two cents.
            • 28042 ☆ A M B ☆
            • 24,524 Posts
            Are you planning to share your "toolkit"?
              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