Ask coding questions

← Back to all posts
How to retrieve env variable in php
Baconman321 (1060)

I set secret to 1 in an env, but when I asked to print it, it didn't work. How do I retrieve an env file for php? I could use some help. Thanks

Answered by heyitsmarcus (342) [earned 5 cycles]
View Answer
heyitsmarcus (342)

@Baconman321, use getenv(). You can use getenv() to output any variable from the .env file.

Let's say you have this in your .env file:


You would call it in the page with:

<?php echo getenv('SECRET'); ?>

Which will echo out the number 1.

You can also store that environment variable into a local PHP variable to modify and use.

$SECRET = getenv('SECRET');
echo ++$SECRET;

Which will echo out the number 2 since it's been incremented by one before being echoed to the page.

heyitsmarcus (342)

@Baconman321, if this solved your problem, please consider marking my answer as the accepted answer to close out this question.