Current Location: Home> Latest Articles> How to Use PHP’s putenv Function to Properly Set and Manage Environment Variables: Step-by-Step Guide

How to Use PHP’s putenv Function to Properly Set and Manage Environment Variables: Step-by-Step Guide

gitbox 2025-09-03

2. Using getenv and $_ENV Together

After setting environment variables, we often need to read them in the program. There are two ways to do this:

  1. Retrieve using getenv()

<span><span><span class="hljs-title function_ invoke__">putenv</span></span><span>(</span><span><span class="hljs-string">"APP_DEBUG=true"</span></span><span>);
</span><span><span class="hljs-keyword">echo</span></span><span> </span><span><span class="hljs-title function_ invoke__">getenv</span></span><span>(</span><span><span class="hljs-string">"APP_DEBUG"</span></span><span>); </span><span><span class="hljs-comment">// Output: true</span></span><span>
</span></span>
  1. Retrieve using the $_ENV superglobal

In some configurations (depending on the variables_order setting in php.ini), values set by putenv may also be synchronized to $_ENV.

<span><span><span class="hljs-title function_ invoke__">putenv</span></span><span>(</span><span><span class="hljs-string">"API_KEY=123456"</span></span><span>);
</span><span><span class="hljs-keyword">echo</span></span><span> </span><span><span class="hljs-variable">$_ENV</span></span><span>[</span><span><span class="hljs-string">"API_KEY"</span></span><span>]; </span><span><span class="hljs-comment">// If enabled, output: 123456</span></span><span>
</span></span>

Note that the availability of $_ENV can vary depending on the server environment and PHP configuration, so it’s generally recommended to use getenv() for better compatibility.


3. Application Scenarios in Web Development

  1. Differentiate between environments

    <span><span><span class="hljs-title function_ invoke__">putenv</span></span><span>(</span><span><span class="hljs-string">"APP_ENV=development"</span></span><span>);
    </span><span><span class="hljs-keyword">if</span></span><span> (</span><span><span class="hljs-title function_ invoke__">getenv</span></span><span>(</span><span><span class="hljs-string">"APP_ENV"</span></span><span>) === </span><span><span class="hljs-string">"development"</span></span><span>) {
        </span><span><span class="hljs-title function_ invoke__">error_reporting</span></span><span>(E_ALL);
        </span><span><span class="hljs-title function_ invoke__">ini_set</span></span><span>(</span><span><span class="hljs-string">"display_errors"</span></span>, </span><span><span class="hljs-number">1</span></span><span>);
    } </span><span><span class="hljs-keyword">else</span></span><span> {
        </span><span><span class="hljs-title function_ invoke__">ini_set</span></span><span>(</span><span><span class="hljs-string">"display_errors"</span></span>, </span><span><span class="hljs-number">0</span></span><span>);
    }
    </span></span>
  2. Database configuration management

    <span><span><span class="hljs-title function_ invoke__">putenv</span></span><span>(</span><span><span class="hljs-string">"DB_HOST=127.0.0.1"</span></span><span>);
    </span><span><span class="hljs-title function_ invoke__">putenv</span></span><span>(</span><span><span class="hljs-string">"DB_USER=root"</span></span><span>);
    </span><span><span class="hljs-title function_ invoke__">putenv</span></span><span>(</span><span><span class="hljs-string">"DB_PASS=secret"</span></span><span>);
    <p></span>$pdo = new PDO(<br>
    "mysql:host=" . getenv("DB_HOST"),<br>
    </span>getenv("DB_USER"),<br>
    </span>getenv("DB_PASS")<br>
    );<br>
    </span></span>

  3. Protect sensitive information
    Store API keys, third-party service tokens, and other credentials in environment variables to avoid hardcoding them in the source code, improving security.


4. Notes and Best Practices

  1. Scope

    • Environment variables set with putenv only apply to the current PHP process and do not persist in the system environment.

    • If you want environment variables to remain after a server restart, you should configure them in system files (e.g., .bashrc or .env files).

  2. Compatibility

    • Behavior is consistent in both CLI and Web modes, but some hosting environments may restrict putenv.

  3. Recommended approach

    • Use a .env file with a PHP loader (such as the vlucas/phpdotenv library) to manage environment variables centrally.

    • Access variables in your code exclusively via getenv to avoid hardcoding.