Question

When setting env variables, how to get them using PHP?

I’m using this command to set environmental variable export COMMIT_ID='x'

When I use printenv I can see it defined. However, PHP can’t see it using getenv('COMMIT_ID')… What am I doing wrong?

Show comments

Submit an answer

This textbox defaults to using Markdown to format your answer.

You can type !ref in this text area to quickly search our full set of tutorials, documentation & marketplace offerings and insert the link!

Sign In or Sign Up to Answer

These answers are provided by our Community. If you find them useful, show some love by clicking the heart. If you run into issues leave a comment, or add your own answer to help others.

Want to learn more? Join the DigitalOcean Community!

Join our DigitalOcean community of over a million developers for free! Get help and share knowledge in Q&A, subscribe to topics of interest, and get courses and tools that will help you grow as a developer and scale your project or business.

Hi there!

What is probably happening is that you set the environment variable with a regular system user (the one you are logged in), but as the web server runs with another user (usually www-data), you don’t have access to that variable from inside your PHP code.

If you are using Ubuntu, you might try setting the variable in the file /etc/environment, so it will be available to all users in the system. This might help: https://help.ubuntu.com/community/EnvironmentVariables#System-wideenvironmentvariables

Thank you, will try

you can set all your environmental variables in /etc/environment and access them from php using getenv function