Setting PHP enviromental variable while running command line script

Setting PHP enviromental variable while running command line script

  1. Blog
  2. 15 November 2026
  3. 3 min read

Stefan Bogdanescu

Founder & Senior Architect

Setting PHP Environmental Variables While Running Command Line Scripts

When executing a PHP script from the command line, developers often need to override default configuration values or define environment-specific settings without modifying the source code itself. A common requirement is to inject an APPLICATION_ENV variable set to staging, for instance. However, attempting to achieve this with the -d flag often results in unexpected behavior.

The syntax php -dAPPLICATION_ENV=staging script.php does not work as intended because of how PHP interprets configuration directives versus environment variables. The -d flag is designed specifically for setting INI file directives, such as display_errors or memory_limit. While some simple functions can be registered as runtime settings in the INI file, complex variable assignments like APPLICATION_ENV=staging are frequently ignored by the engine if they do not map directly to a recognized configuration option. Furthermore, simply appending this flag does not automatically populate the $_ENV or $_SERVER superglobals with the specified key for your application logic to consume.

To properly set an environment variable that your PHP script can access via getenv() or $_ENV, you must pass the variable through the shell before invoking the PHP interpreter. The correct approach relies on the shell's variable expansion mechanism rather than PHP's internal configuration flags.

If you are using a Unix-like operating system (Linux, macOS), you should prefix the command with the variable assignment. Ensure that there is a space between the variable definition and the php command to prevent the shell from interpreting it as a single argument or flag. The correct syntax looks like this:

APPLICATION_ENV=staging php script.php

Alternatively, if you prefer setting the variable for the current session without relying on shell expansion, you can use double quotes and escape the dollar sign if necessary, though simple assignment usually suffices. This method ensures that the APPLICATION_ENV string is available in the process environment before PHP starts parsing the script. Consequently, when your code executes getenv('APPLICATION_ENV'), it will return staging.

For Windows users, the mechanism differs slightly due to the CMD or PowerShell shell behavior. In standard Command Prompt (cmd.exe), you must use the set command to define the variable before calling PHP:

set APPLICATION_ENV=staging
php script.php

In PowerShell, the syntax is more direct but requires ensuring the variable is defined in the current scope:

$env:APPLICATION_ENV="staging"
php script.php

It is crucial to understand the distinction between PHP's -d flag and shell environment variables. The -d flag modifies the runtime configuration for specific directives defined in php.ini. If your application framework, such as Laravel or Symfony, checks for APP_ENV specifically via getenv('APP_ENV'), setting APPLICATION_ENV with the -d flag will not help unless the framework explicitly looks for a directive named application_env in its configuration loader. Most modern frameworks rely on the shell environment or a dedicated .env file parser, not the PHP INI system, for these specific keys.

Another practical consideration is the scope of these variables. Variables set via set in CMD or exported variables in bash only persist within that terminal session. If your CI/CD pipeline requires this setting, you must ensure the environment variable is injected into the build agent's context before the PHP process spawns. Tools like Docker or Kubernetes handle this naturally by mounting environment variables at the container level, where they are automatically accessible to any command run inside that container.

If you find yourself needing to pass these values programmatically from within PHP itself—for example, if you cannot modify the shell script—using putenv() is an option. However, this requires writing code to set the variable, which contradicts the goal of external configuration. Therefore, relying on the execution environment remains the cleanest architectural choice for separating configuration from logic.

Our network