Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is it possible to use environment variables in php.ini?

Tags:

Rather than hard-wiring some paths in my php.ini configuration, I'd like to configure them using system variables that are shared in some other places such as my Apache configuration. I've done some searching and couldn't find the right mix of keywords to discover if there's a way to do this.

Does anyone know if this can be done?

upload_tmp_dir = $SCRATCH_HOME/uploads

Now SCRATCH_HOME can be exported in the environment as /tmp or /var/scratch or whatever I want it to be.

like image 378
Joe Holloway Avatar asked May 13 '09 14:05

Joe Holloway


People also ask

Can I use .env in PHP?

An . env file is a plain text file which contains environment variables definitions which are designed so your PHP application will parse them, bypassing the Apache, NGINX and PHP-FPM. The usage of . env files is popular in many PHP frameworks such as Laravel which has built-in support for parsing .

How can I get environment variable in PHP?

Using getenv() In addition to using PHP's Superglobals, you can also use getenv() to retrieve an environment variable. If the function is called without an argument, then it returns all available environment variables. If an argument is passed, however, the value of an environment variable with that name is returned.

Can I use variables in .env file?

You can set default values for environment variables using a .env file, which Compose automatically looks for in project directory (parent folder of your Compose file). Values set in the shell environment override those set in the .env file.

What is $_ env in PHP?

$_ENV is another superglobal associative array in PHP. It stores environment variables available to current script. $HTTP_ENV_VARS also contains the same information, but is not a superglobal, and now been deprecated. Environment variables are imported into global namespace.


1 Answers

Documented officially: https://www.php.net/manual/en/configuration.file.php#example-36

Before I begin, I just want to specify my configurations: I'm using Windows 7-64bit, PHP 5.4.3, Apache HTTP Server 2.2.x, I've set my environmental variable PHP_HOME=C:\tools\php-5.4.3 (PHP installation directory). I use the variable in my httpd.conf and php.ini file

Note: I will be omitting some text for brevity.

In the httpd.conf file

# For PHP 5 do something like this:
LoadModule php5_module "${PHP_HOME}/php5apache2_2.dll"
AddType application/x-httpd-php .php

# configure the path to php.ini
PHPIniDir "${PHP_HOME}"

In the php.ini file

; Directory in which the loadable extensions (modules) reside.
; http://php.net/extension-dir
; extension_dir = "./"
; On windows:
extension_dir = "${PHP_HOME}/ext"
like image 179
Hlulani Avatar answered Oct 21 '22 14:10

Hlulani