Environment Variables and .env Files
Manage sensitive config with .env files and dotenv libraries in PHP.
Environment Variables and .env Files is a free PHP Academy lesson on CoddyKit — lesson 2 of 4. You can read the complete lesson below for free — then practise it hands-on in the browser with a built-in code editor and a 24/7 AI tutor. It is part of the PHP Academy learning path, one of 4 lessons in the course, and your progress syncs across the web and the CoddyKit app.
Why Use Environment Variables?
Hardcoding credentials or environment-specific config in source code is insecure and makes deployments fragile. Environment variables separate config from code.
The .env File
A .env file contains key-value pairs loaded into the environment at boot. Add it to .gitignore — NEVER commit it to version control.
# .env
APP_ENV=production
APP_KEY=base64:...
DB_HOST=127.0.0.1
DB_NAME=myapp
DB_USER=myapp_user
DB_PASS=supersecret
REDIS_HOST=127.0.0.1
MAIL_HOST=smtp.mailtrap.ioLaravel .env Integration
Laravel uses vlucas/phpdotenv to load .env automatically. Access values with the env() helper.
<?php
$host = env("DB_HOST", "127.0.0.1"); // "127.0.0.1" is the defaultconfig() vs env()
Use env() only in config/*.php files. Everywhere else, use config("database.host") — this works correctly with configuration caching.
<?php
// In config/database.php:
"host" => env("DB_HOST", "127.0.0.1")
// In your service classes:
$host = config("database.connections.mysql.host");Caching Config
Cache config for production to skip parsing .env on every request.
$ php artisan config:cache
# After changing .env:
$ php artisan config:clearPlain PHP with phpdotenv
Use vlucas/phpdotenv outside of Laravel.
$ composer require vlucas/phpdotenv
<?php
$dotenv = Dotenv\Dotenv::createImmutable(__DIR__);
$dotenv->load();
$host = $_ENV["DB_HOST"];Required Variables
Enforce required env vars at boot to catch missing config early.
<?php
$dotenv->required(["DB_HOST", "DB_NAME", "DB_USER", "DB_PASS"]);
$dotenv->required(["DB_PORT"])->isInteger();.env.example File
Commit a .env.example file with all variable names but no values. Developers copy it to .env and fill in their own values.
Server-Level Env Vars
In production, set environment variables at the server level (systemd service files, Docker --env flags, Kubernetes secrets) instead of a .env file.
# systemd service:
[Service]
Environment="APP_ENV=production"
Environment="DB_HOST=db.internal"Secrets Management
For sensitive credentials (API keys, certificates) consider a secrets manager: HashiCorp Vault, AWS Secrets Manager, or GitHub Actions secrets for CI.
Summary
Store all environment-specific config in .env. Never commit it. Use config() in application code; env() only in config files. Cache config in production with php artisan config:cache.
Quick Check
Where should you call env() in a Laravel app?
Frequently asked questions
Is the “Environment Variables and .env Files” lesson free?
Yes — the full text of “Environment Variables and .env Files” is free to read here on the web, and the PHP Academy course includes 4 lessons in total. To practise it interactively (a built-in code editor and a 24/7 AI tutor) and unlock the rest of the PHP Academy course, upgrade to CoddyKit PRO.
What will I learn in “Environment Variables and .env Files”?
Manage sensitive config with .env files and dotenv libraries in PHP. You practise PHP Academy with hands-on code you run directly in the browser, and a 24/7 AI tutor answers your questions as you work through the lesson.
Do I need any experience to start PHP Academy?
No prior experience is required. PHP Academy on CoddyKit is structured for beginners through advanced learners; this is — lesson 2 of 4, so you can start here or from the beginning and move at your own pace.
How long does the “Environment Variables and .env Files” lesson take?
Most CoddyKit lessons take about 5–10 minutes. Each one is bite-sized and interactive, so you make steady progress and pick up exactly where you left off across the web and the app.
Can I write and run code in this PHP Academy lesson?
Yes. Every PHP Academy lesson includes a built-in code editor, so you write and run real code right in your browser and get instant AI feedback — no local setup required.
All lessons in this course
- Nginx and PHP-FPM Configuration
- Environment Variables and .env Files
- Deploying with GitHub Actions
- Dockerizing a PHP Application