Dotenv\Loader::setEnvironmentVariable PHP Method

setEnvironmentVariable() public method

This is done using: - putenv, - $_ENV, - $_SERVER. The environment variable value is stripped of single and double quotes.
public setEnvironmentVariable ( string $name, string | null $value = null ) : void
$name string
$value string | null
return void
    public function setEnvironmentVariable($name, $value = null)
    {
        list($name, $value) = $this->normaliseEnvironmentVariable($name, $value);
        // Don't overwrite existing environment variables if we're immutable
        // Ruby's dotenv does this with `ENV[key] ||= value`.
        if ($this->immutable && $this->getEnvironmentVariable($name) !== null) {
            return;
        }
        // If PHP is running as an Apache module and an existing
        // Apache environment variable exists, overwrite it
        if (function_exists('apache_getenv') && function_exists('apache_setenv') && apache_getenv($name)) {
            apache_setenv($name, $value);
        }
        if (function_exists('putenv')) {
            putenv("{$name}={$value}");
        }
        $_ENV[$name] = $value;
        $_SERVER[$name] = $value;
    }

Usage Example

Example #1
0
 /**
  * @override
  * @inheritDoc
  */
 public function setEnvironmentVariable($name, $value = null)
 {
     if (strpos($name, 'INI_') === 0) {
         list($name, $value) = $this->normaliseEnvironmentVariable($name, $value);
         $normalized = str_replace('INI_', '', $name);
         $normalized = strtolower($normalized);
         $this->invoker->call('ini_set', [$normalized, $value]);
     }
     parent::setEnvironmentVariable($name, $value);
 }
All Usage Examples Of Dotenv\Loader::setEnvironmentVariable