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

    I have a problem with a function() in my snippet. I use the following code:

    <?php
    
    $op2 = blabla;
    $op3 = blabla;
    
    function foo($op1)
    {
      global $op2, $op3;
    
        echo $op1;
        echo $op2;
        echo $op3;
    }
    foo('test');
    ?>


    If i execute the snippet the only output is: ’test’ But the two other vars are not outputted.

    What am i doing wrong?

    Thank for your help!
      • 16034
      • 107 Posts
      I don’t know if this is why your script doesn’t work, but you should get a warning from PHP. (Depending on your error_reporting-level).

      You should put quotes around the string blabla:
      <?php
      
      $op2 = 'blabla';
      ...

        MODx snippet-glossary 101:
        Ditto = Content Lister -- Wayfinder == Menu Builder -- Jot = Comment Control
        • 22518
        • 3 Posts
        No, i don’t get a php warning...

        Also adding the quotes will not help...

        Pascal
          • 30223
          • 1,010 Posts
          snippets are executed using eval(). You simply have a scoping problem... Try:
          <?php
          
          $GLOBALS['op2'] = "blabla";
          $GLOBALS['op3']  = "blabla";
          
          function foo($op1)
          {
            global $op2, $op3;
          
              echo $op1;
              echo $op2;
              echo $op3;
          }
          foo('test');
          ?>
          
          
            • 22715
            • 1 Posts
            tnx, that fixes my problem!