How to Set Global Environment Variables for PHP

How to make global variables in php

Your question is kinda ambiguous for me. If you mean setting a variable from one file then making it available from all your php files you can put it in one file then include the file it in all your scripts. Put it on the top. Or you can put it inside an $_SESSION variable. This is the simplest solution I know. :)

Sample: Assuming all files are in the root directory

vars.php:

$globalVar = 2;
//or
$_SESSION['global_var'] = 2; // if a session exists

otherfile.php:

include 'vars.php';
echo $globalVar; //outputs 2
//or
echo $_SESSION['global_var']; //outputs 2

I'm not sure if this is what you are trying to achieve :D

How to set the env variable for PHP?

You need to put the directory that has php.exe in you WAMP installation into your PATH. It is usually something like C:\wamp\xampp\php

How to set global variables in Laravel?

All these values should be in your .env file. Then you need to read these values in a config file by using env() helper:

'variable' => env('SOME_DATA'),

And then you'll be able to use these values globally with:

config('config_file.variable')

How to set up an env variable in PHP?

you can use putenv

putenv("KEY=VALUE");


Related Topics



Leave a reply



Submit