How To Read .env File Values in PHP
Laravel loads .env files automatically, but plain PHP doesn't. Learn how to read .env file values in custom PHP scripts.
Ashwin Mehta
3 min read

If you've worked with Laravel, you're probably familiar with the .env file. You define your environment variables there, and Laravel takes care of loading them automatically.
But what if you're building a PHP application without a framework? Or perhaps you have a custom PHP script deployed on PieApp and want to read configuration values from your .env file.
PHP doesn't load .env files automatically. You need to load them yourself before accessing their values.
Let's see how to do that in a PHP application deployed on PieApp.
Understanding the .env file
A .env file is commonly used to store application configuration and environment-specific values.
For example, your application might have a .env file in its root directory (/app/.env) containing:
APP_NAME=My PHP Application
APP_ENV=production
APP_DEBUG=false
DB_HOST=localhost
DB_NAME=my_database
DB_USER=my_user
DB_PASSWORD=my_secret_password
API_KEY=your_api_key
Instead of hardcoding these values into your PHP files, you can keep them in .env and load them when your application starts.
This makes it easier to manage configuration without changing your application code.
Install phpdotenv (recommended)
You can still read .env without phpdotenv
However, easiest and recommended way to read .env files in PHP is using the vlucas/phpdotenv package.
If your application uses Composer, install it with:
composer require vlucas/phpdotenv
This package handles reading the .env file and making its values accessible through PHP's environment variable functions.
Load the .env file in your PHP application
Since PieApp uses /app/ as the application root directory, your .env file should be located at:
/app/.env
You can load it using the following code:
<?php
require_once __DIR__ . '/vendor/autoload.php';
$dotenv = DotenvDotenv::createImmutable('/app');
$dotenv->load();
The createImmutable('/app') method tells phpdotenv where to look for the .env file.
Once loaded, you can access the values using $_ENV or getenv().
For example:
$appName = $_ENV['APP_NAME'] ?? 'My Application';
$dbHost = $_ENV['DB_HOST'] ?? 'localhost';
$apiKey = $_ENV['API_KEY'] ?? '';
echo $appName;
That's it! Your custom PHP application can now read values from /app/.env.
Access environment variables from anywhere
You don't need to load the .env file every time you want to access a value.
A good practice is to load it once when your application starts, usually in your main entry point or a shared bootstrap file.
For example, create a bootstrap.php file:
<?php
require_once __DIR__ . '/vendor/autoload.php';
$dotenv = DotenvDotenv::createImmutable('/app');
$dotenv->load();
Then include it in your application:
<?php
require_once __DIR__ . '/bootstrap.php';
echo $_ENV['APP_NAME'];
You can now access your environment variables across your application without repeatedly loading the file.
A small note: With createImmutable(), phpdotenv populates $_ENV and $_SERVER. If you specifically want to use getenv(), use createUnsafeImmutable() instead.
What if you don't want to install a package?
If you have a very small PHP application and don't want to install Composer dependencies, you can also read the file manually.
PHP provides parseinifile(), which can read files in INI format.
For a simple .env file:
<?php
$env = parse_ini_file('/app/.env');
$appName = $env['APP_NAME'] ?? 'My Application';
$dbHost = $env['DB_HOST'] ?? 'localhost';
echo $appName;
This works for simple key-value configurations, but it isn't a complete replacement for phpdotenv. It doesn't support all dotenv features, and you should be careful with special characters and quoting.
For most applications, I'd recommend using vlucas/phpdotenv.
A few things to keep in mind when deploying on PieApp
When deploying your PHP application on PieApp, keep these points in mind:
- File location: Keep your .env file in the application root, /app/.env.
- Absolute path: Using /app when initializing phpdotenv ensures it looks in the correct directory.
- Don't commit secrets: Add .env to your .gitignore file so database passwords, API keys and other secrets aren't pushed to your Git repository.
- Load it early: Load the .env file before initializing database connections or other services that depend on environment variables.
Also, make sure your .env file isn't publicly accessible through your web server.
Conclusion
Reading .env values in plain PHP isn't difficult. Unlike Laravel, PHP doesn't automatically load environment variables from a .env file, so you need to handle that yourself.
For a production application deployed on PieApp, using vlucas/phpdotenv is a straightforward and reliable approach.
Once loaded, you can access your configuration through $_ENV just as you would in a framework-based application.


Comments
No comments yet. Questions and corrections are welcome.