Logo Questions Linux Laravel Mysql Ubuntu Git Menu

How do I get all environment variables as an array in PHP?

Title says it. I want to use this with proc_open, to append some variables to the current environment.

$current_env = get_all_env_vars_magically();
$env = array_merge($current_env, $new_vars);
$ph = proc_open($command, array(1 => array('pipe', 'w')), 
    $pipes, dirname(__FILE__), $env);

Edit: $_ENV is empty/not populated by default. $_SERVER contains so much more than env vars.

like image 816
Znarkus Avatar asked Dec 20 '12 14:12


1 Answers

Since PHP7.1 the varname can now be omitted to retrieve an associative array of all environment variables.

try getenv() https://3v4l.org/fkFoR

PHP Docs: https://www.php.net/manual/en/function.getenv.php

like image 150
PATROMO Avatar answered Oct 22 '22 15:10