FL-RKS Posted August 10, 2021 Share Posted August 10, 2021 i need a way to charge get enviroment variables stored in a .env file i know that there are libraries to do that, but is takes a lot of time, there is a native way to do that in php in less time? Quote Link to comment https://forums.phpfreaks.com/topic/313522-get-enviroment-variables-from-env-file/ Share on other sites More sharing options...
ginerjm Posted August 10, 2021 Share Posted August 10, 2021 What do you consider 'environment variables'? Not familiar with that term since my days with MS-DOS. And did you mean to say 'charge' or possibly 'change'? Quote Link to comment https://forums.phpfreaks.com/topic/313522-get-enviroment-variables-from-env-file/#findComment-1588957 Share on other sites More sharing options...
FL-RKS Posted August 10, 2021 Author Share Posted August 10, 2021 i refeer to the variables stored in a .env file in the root of your proyect, like: DB_HOST="localhost" DB_USER="root" DB_NAME="test" DB_PASSWORD="123abs" API_KEY="secret_key" there is a superglobal variavle called $_ENV, but is empty even if the .env file is valid Quote Link to comment https://forums.phpfreaks.com/topic/313522-get-enviroment-variables-from-env-file/#findComment-1588959 Share on other sites More sharing options...
ginerjm Posted August 10, 2021 Share Posted August 10, 2021 (edited) So that is part of a 3rd party app that you are using? Not familiar with how those apps do that since I have never used one. But - they look like constants. Maybe search the php manual for CONSTANTS and see what it tells you. (PS - those lines all need semis) Edited August 10, 2021 by ginerjm Quote Link to comment https://forums.phpfreaks.com/topic/313522-get-enviroment-variables-from-env-file/#findComment-1588960 Share on other sites More sharing options...
Barand Posted August 10, 2021 Share Posted August 10, 2021 You could treat that .env file as an ini file. For example... $envs = parse_ini_file('myproject.env'); echo $envs['API_KEY']; //-> secret key Quote Link to comment https://forums.phpfreaks.com/topic/313522-get-enviroment-variables-from-env-file/#findComment-1588967 Share on other sites More sharing options...
requinix Posted August 10, 2021 Share Posted August 10, 2021 If you're talking about Laravel's .env file then you should be using Laravel's procedures to access its data. https://laravel.com/docs/8.x/configuration#environment-configuration Quote Link to comment https://forums.phpfreaks.com/topic/313522-get-enviroment-variables-from-env-file/#findComment-1588989 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.