Is there a way in PHP to pass variables from a function to the scope in which it was called? I understand that there is a return , but I am interested in simply setting an external variable from a function (and except for $GLOBALS ).

  • and than usual methods do not suit? For example <? global $ a; $ a + = $ b; ?> in the function itself - zenith
  • @zenith, just wondering. It is possible and through return , of course. But $GLOBALS generally uncomfortable. - Oleg Arkhipov
  • Well, you can specify the visibility of the variable when you call it in a function. If I'm wrong throw me a pineapple. - zenith
  • @zenith, how? - Oleg Arkhipov

1 answer 1

There are several ways:

  1. Through $GLOBALS . (although you don't like it)
  2. Through global $param . (for some reason you didn't like it either)
  3. Via return array('param' => $param) and parse it.
  4. Through the transfer of the function of parameters as references: function a(&$param){$param = 123;} .

Did I forget something?

  • Stop, №2 - it's the same for holding the variable IN function, is it not? Or does it create an external link inside? I need to change the external from the inside. - Oleg Arkhipov