Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Declaring global variable with php.ini

Is it possible to keep variables in php.ini file. Like that we do with the web.config in .net. I like to keep a flag type variable in the php.ini and use it to different projects.

like image 851
Zstream Avatar asked Feb 19 '11 18:02

Zstream


People also ask

How can store global variable in PHP?

$GLOBALS is a PHP super global variable which is used to access global variables from anywhere in the PHP script (also from within functions or methods). PHP stores all global variables in an array called $GLOBALS[index]. The index holds the name of the variable.

How do you declare global variables in pho?

Global variables refer to any variable that is defined outside of the function. Global variables can be accessed from any part of the script i.e. inside and outside of the function. So, a global variable can be declared just like other variable but it must be declared outside of function definition.

How do you declare a global variable?

The global Keyword Normally, when you create a variable inside a function, that variable is local, and can only be used inside that function. To create a global variable inside a function, you can use the global keyword.

Should I use global variables in PHP?

There is no need to do global $variable; to access it within functions or methods. Unlike all of the other superglobals, $GLOBALS has essentially always been available in PHP.


2 Answers

It's not possible to set user-level variables within a plain php.ini file (or the .htaccess equivilents). There are some PECL modules that do allow that, such as hidef (http://pecl.php.net/package/hidef) - though these would need to be installed on every installation you use.

Including (or pre-including) a file with auto_prepend_file is quite possible - though that would be on every PHP request.

What is frequently done is setting an environment variable as part of the webserver process, which can be read from PHP. In Apache this is quite easy, with the SetEnv module.

SetEnv PRODUCTION_SERVER 1

And accessing it in PHP:

if ($_ENV['PRODUCTION_SERVER']) {...}  // or getenv('PRODUCTION_SERVER')
like image 192
Alister Bulman Avatar answered Sep 18 '22 12:09

Alister Bulman


Have you looked at get_cfg_var()?

I needed to do something similar, and this was able to do it for me.

like image 33
Ascherer Avatar answered Sep 19 '22 12:09

Ascherer