0

this is my code and it works quite well in normal PHP but not working in Laravel helpers.php file

$t = [13];
function subtraction()
{
    global $t;
    $t[] = 14;
    var_dump($t,gettype($t));
} 
subtraction();

result in normal PHP file is :

array:2 [
  0 => 13
  1 => 14
]
"array"

result in Laravel is :

array:1 [
  0 => 14
]
"array"

I hope help me... Thanks

  • 2
    How are you including that helper file? The use of `global` is sometimes a sign of code smell. There is probably a better way to do what you're doing. Perhaps take a step back and tell us what you're trying to achieve. – waterloomatt Sep 12 '21 at 19:13
  • @pichanakian what are you trying to achieve ? Using `global` in Laravel is a no-no, you are doing something wrong. When using Laravel forget about `$_GLOBAL` or `$_POST` or anything similar. You have to use the framework to solve this. Could you explain more why are you trying to use `global` like that ? – matiaslauriti Sep 12 '21 at 21:07

1 Answers1

0

You can declare any global variable in .env file while using laravel. e.g

GLOBAL_T = 13;

And you can call it in helper file like this:

env('GLOBAL_T');

If you want to define an array as global then please visit this link answer is provided here

Kaleem Shoukat
  • 811
  • 6
  • 14
  • 1
    A config file would be better suited for this, assuming the GLOBAL_T doesn't change in a different **env**ironment. – brombeer Sep 12 '21 at 19:24
  • @brombeer @kaleemshoukat a `config` file is **MANDATORY** as when you are on `production`, `env()` does not work anymore once config is cached... – matiaslauriti Sep 12 '21 at 21:05