I have a simple Symfony project, using "symfony/dotenv": "4.3.*",
in the composer.json
and trying to read in the value of an environment variable.
This is my command:
var_dump($_ENV['MY_NEW_VAR']);
This is my .env
file
MY_NEW_VAR=testing
Upon executing the command I get the following
string(7) "testing"
All good so far.... But now if I want to use environment variables to override the setting it returns an error. First I set the environment variable
export MY_NEW_VAR="something_else"
Then execute the command again, with the following results
[ErrorException]
Notice: Undefined index: MY_NEW_VAR
If I remove the environment variable using unset MY_NEW_VAR
and execute the command again I get the value from the .env
file
I would expect the environment variable to override the .env
setting as explained in the comments of the .env
file
# In all environments, the following files are loaded if they exist,
# the later taking precedence over the former:
#
# * .env contains default values for the environment variables needed by the app
# * .env.local uncommitted file with local overrides
# * .env.$APP_ENV committed environment-specific defaults
# * .env.$APP_ENV.local uncommitted environment-specific overrides
#
# Real environment variables win over .env files.
Why does it not work with environment variables? Am I missing something?