Making global var from inside a function in PHP

后端 未结 3 1475
栀梦
栀梦 2021-01-23 08:16

I am trying to define dynamically variables. I am using a function for this, but I don\'t know how to define the new var as global (because it never created before the function)

3条回答
  •  花落未央
    2021-01-23 08:31

    Try this:

    function doSomething() {
      global $x;
      $x = 5;
    }
    

    If you prefer to save a couple of bytes, you can use the $_GLOBALS array for this:

    $_GLOBALS['x'] = 5;
    

提交回复
热议问题