Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

KPoruba's avatar

.env variables returns null

Hi,

when I would get a variables from .env file using env() function, always returns null. I tried cache:clear, config:cache, finally i change in correct php.ini value from variables_order = "GPCS" to variables_order = "EGPCS". From this moment returns $_ENV array of data, but not include data from .env file.

I could't get data from .env anyway. Can anyone help me?

Thanks, have a good day!

0 likes
12 replies
Snapey's avatar

The names of the elements are case sensitive. Make sure you query accurately.

example usage;

/Volumes/Macintosh HD/webdev/novate/csdb/ $ php artisan tinker
Psy Shell v0.7.1 (PHP 5.5.31 — cli) by Justin Hileman
>>> env('DB_HOST')
=> "127.0.0.1"
>>>
KPoruba's avatar

Yes, i treid case sensitivity, but no success :( Your example is awesome, I don't know tinker before. At this moment thing seems like a .env file can't cache, but in /bootstrap/cache/config.php is a PART of .env file already cached. But from tinker i can't access any variable from .env file, like DB_HOST etc.

Laravel probably takes it from /bootstrap/cache/config.php and at command php artisan config:cache caches only PART of .env file. But again this values are not accessible from env() command :(

Thanks

pmall's avatar

Try to restart your webserver

1 like
KPoruba's avatar

Hi, restarting web server (apache2) I tried many times, same sudo service apache2 reload. But no effect :( I totally don't know, how is this possible, how can Laravel cache only PART of .env file and why this variables are not accessible from env() command / function.

I tried access to .env variables in other project based on Laravel, there it works. Superglobal variable $_ENV is filled with values from .env file and varibles are accessible from env() function.

drbojingle's avatar

Were you ever able to resolve this issue? I'm having the same trouble and nothing seems to work.

Damarpe's avatar

For me what worked was: php artisan config:clear

6 likes
nouvre's avatar

This was happening to me and php artisan config:clear worked.

1 like
Nash's avatar

If you cache the config, you can't use env() in your application (only in config files). Use config() instead and env() only in config files (or don't cache the config).

If you execute the config:cache command during your deployment process, you should be sure that you are only calling the env function from within your configuration files. Once the configuration has been cached, the .env file will not be loaded and all calls to the env function will return null.

Configuration Caching

13 likes
Nash's avatar

@lmatab You're welcome. Please mark the thread as solved to help others.

Please or to participate in this conversation.