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

    I’ve been playing around with a module and some snippets learning how modx works. I can usually figure out what most of the code is doing but now I’m a bit stumped...

    In this code: ’ $items = (isset($items)) ? intval($items) : -1; ’ can someone explain what the ’ : ’ means.

    I’ve looked in the Php refrence manual but can’y find any thing there. I’m guessing it means something like ’or’, would that be right?

    Thanks
      • 28042 ☆ A M B ☆
      • 24,524 Posts
      that is the ternary operator. If the part before the ? is true, then the first part after the ? and before the : will be used, while if it is false then the part after the : will be used.
      http://il2.php.net/operators.comparison

      In the example you gave, it the variable $items is set then the integer value of $items used, if it’s not set then -1 is used. An if-else block could also be used, but once you learn to read these they become second nature.
        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
        • 13632
        • 22 Posts
        Hi sottwell,

        I get it grin that’s so simple. I did notice quite a lot of them being used in modx and now I see why. The if-else blocks seem long winded compared to this method.

        once you learn to read these they become second nature.

        I’m off to make second nature of these pearls of wisdom, Thank you for your speedy answer.