Load Environment Variables from (.env) File using PHP Tutorial
In this tutorial, you will learn how to Load the Environment Variables from a (.env) File using PHP Language. The tutorial aims to provide IT/CS students and new programmers with a reference to learn for loading environment variables in PHP. Here, a step-by-step tutorial and sample snippets are provided to understand the process for achieving the goal of this tutorial. A sample source code zip file is also provided and is free to download.
What are Environment Variables?
An environment variable is a value that is dynamically set and utilized throughout the program to control how a program behaves on each device. They are a component of the setting in which software runs. It is mostly used to store the variables that are needed in a web application and give you the ability to configure a value in your code without opening the file script where the variable is/are called. The Environment Variables give you the benefits to increase security and Simple to maintain and flexible code.
Example
Here's an example of an environment variable in a (.env) File.
- ##MY API Credentials
- web_api_client_id = 78895asda25as54dsaaa5wgb
- web_api_seckret_key = 668a7dasd8aw85xc8778a78wd$!@8
How to Load Environment Variables from (.env) file using PHP?
Here are the following steps to load the environment variables from the (.env) file using PHP:
Step 1
Identify the path of the (.env) file.
- <?php
- ?>
Step 2
Check if the (.env) file exists. Also, check if the file permission is writable and readable.
- <?php
- //Check .envenvironment file exists
- throw new ErrorException("Environment File is Missing.");
- }
- //Check .envenvironment file is readable
- throw new ErrorException("Permission Denied for reading the ".($env_file_path).".");
- }
- //Check .envenvironment file is writable
- throw new ErrorException("Permission Denied for writing on the ".($env_file_path).".");
- }
Step 3
List all the variables from the (.env) file and its value. To do that, you must read the file in each line to capture the variables and skip the comment data and empty lines. You can do that using the PHP fopen() built-in function
- <?php
- // Open the .en file using the reading mode
- if($fopen){
- //Loop the lines of the file
- // Check if line is a comment
- // If line is a comment or empty, then skip
- continue;
- // Split the line variable and succeeding comment on line if exists
- // Split the variable name and value
- $var_arrs[$env_name] = $env_value;
- }
- // Close the file
- }
Step 4
Lastly, store and set the value of the environment variable in the PHP superglobal associative array. Using $_ENV or putenv().
- <?php
- foreach($var_arrs as $name => $value){
- //Using putenv()
- //Or, using $_ENV
- $_ENV[$name] = $value;
- // Or you can use both
- }
- ?>
Retrieving Environment Variables Values
Here's the sample snippet on how to retrieve the value of the environment variable stored in PHP's superglobal array.
- <?php
- //For putenv(), use getenv()
- //For $_ENV
- echo $_ENV['web_api_client_id'];
- ?>
PHP Class to Load Environment Variables
The script below is a simple and working PHP Class that I created for loading the environment variables from (.env) files into an application. Feel free to use it for your own PHP Projects.
- <?php
- class DotEnvironment {
- private $path;
- private $tmp_env;
- function __construct($env_path = ""){
- // Check if .env file path has provided
- throw new ErrorException(".env file path is missing");
- }
- $this->path = $env_path;
- //Check .envenvironment file exists
- throw new ErrorException("Environment File is Missing.");
- }
- //Check .envenvironment file is readable
- }
- $this->tmp_env = [];
- if($fopen){
- // Check if line is a comment
- continue;
- $this->tmp_env[$env_name] = $env_value;
- }
- }
- $this->load();
- }
- function load(){
- // Save .env data to Environment Variables
- foreach($this->tmp_env as $name=>$value){
- $_ENV[$name] = $value;
- }
- // print_r(realpath($this->path));
- }
- }
- ?>
The usage of the DotEnvironment Class is like the following snippet.
There you go! That's the end of this tutorial. I hope this Load Environment Variables from (.env) File using PHP Tutorial helps you with what you are looking for and that you'll find this useful for your current and future PHP Projects.
Explore more on this website for more Tutorials and Free Source Codes.
Happy Coding =)
Add new comment
- 10753 views