1

I'm working within Smarty 2.6.27. There is something that prevents PHP functions from accessing global variables:

{php}
  $a = "should be global";

  function test(){
    global $a;
    echo $a;
  }
  test();
{/php}

But the variable $a never makes it into the function. I've also done this:

{php}
  $a = "should be global";

  foreach ($GLOBALS as $key => $value) { 
     echo $key . "-" . $value;
  } 
{/php}

But again, $a does not make it into the list of PHP globals. It seems that this is because Smarty runs PHP in some odd context--for example, the documentation mentions this:

To access PHP variables in {php} blocks you will need to use the PHP global keyword.

I don't really need to get access to other PHP globals, and I don't particularly want the scope of variables created here to go wider, I simply need to figure out how to use variables defined outside of the functions in the function. There will be a lot of these variables, so passing them as parameters is impractical.

brentonstrine
  • 21,694
  • 25
  • 74
  • 120
  • This is how you `{assign}` a global variable in Smarty: `{assign var=foo value="bar" scope="global"}` – Amal Murali Oct 19 '13 at 20:40
  • I'm working within PHP within Smarty, so this seems a little cumbersome. I would have to turn PHP off, assign the Smarty variable, then turn PHP back on--would the Smart variable even be available to PHP as a variable after that? – brentonstrine Oct 19 '13 at 20:53
  • I just tried `{assign var=a value="works?" scope="global"}{php}echo "{/php}{$a}{php}";{/php}` but it does not `echo` out anything. I don't think Smarty and PHP are meant to play together this way. – brentonstrine Oct 19 '13 at 21:23
  • 1
    The "outside the box" answer here is that you should not be using `{php}` blocks in Smarty in the first place. The whole point of Smarty is that the template logic can be written in Smarty; mixing in raw PHP defeats the whole object. – IMSoP Oct 19 '13 at 21:59
  • @IMSoP I hear you. I don't know WHMCS templates require Smarty, and yet the internal API for WHMCS doesn't work without straight up PHP. – brentonstrine Oct 19 '13 at 22:20
  • @brentonstrine Then it might be good to ask more specifically what you are trying to achieve in WHMCS; perhaps there is a less hacky solution? – IMSoP Oct 19 '13 at 22:24
  • @IMSoP You're right, but I am not really sure how to phrase the question--the official documentation for WHMCS encourages `{php}` all throughout. I think it's just poorly designed software. – brentonstrine Oct 19 '13 at 22:34
  • @brentonstrine Heh. Maybe the solution is to avoid that software then! ;) – IMSoP Oct 19 '13 at 22:37

1 Answers1

0

Firstly, for the record I strongly recommend avoiding both {php} blocks in Smarty, and global variables. Try to find some other solution to the underlying problem.

That out of the way, you quote this line from the manual, but you haven't followed its advice:

To access PHP variables in {php} blocks you will need to use the PHP global keyword.

What it means is this:

{php}
  global $a;
  $a = "should be global";
{/php}

Without that, the variable $a is scoped to whatever function context the generated code is run in. There is no special consideration for "nested" functions or scopes in PHP, so a variable is either global, or visible only inside that one function.

IMSoP
  • 89,526
  • 13
  • 117
  • 169
  • Ah, I'd tried that but I guess I had another problem that made it seem like it wasn't working. This fixed it, though I really do with it didn't have to be GLOBAL global. Oh well! – brentonstrine Oct 20 '13 at 01:42