DEV Community

Sachin Sanchania
Sachin Sanchania

Posted on

Simplify PHP Environment Management with CoreEnv

Managing environment variables in PHP projects can quickly become messy — especially when juggling multiple .env files or different environments (local, staging, production). That’s where CoreEnv comes in.

CoreEnv is a lightweight and dependency-free PHP library for loading and managing environment variables effortlessly.
It helps you read .env files, cast values to appropriate types, and keep your configuration clean and secure.


🚀 What is CoreEnv?

CoreEnv is a PHP utility that loads configuration values from .env files into your application environment ($_ENV, $_SERVER, and putenv()).
It provides helper methods to retrieve variables safely with fallback defaults, boolean and numeric casting, and validation.

This makes it ideal for:

  • 🧩 Standalone PHP projects
  • 🧱 Frameworks like Laravel, CodeIgniter, Symfony, or custom setups
  • 🛠 CLI scripts or cron jobs that need environment configuration

🧰 Key Features

  • 📂 Load .env files easily
  • 🔐 Automatic type casting (true, false, null, numbers)
  • 🧩 Retrieve environment variables with default values
  • ⚙️ Supports nested or multiple .env files
  • 🚫 Zero dependencies — pure PHP
  • 💡 Minimal and fast

⚙️ Installation

Install CoreEnv using Composer:

composer require sachin-sanchania/coreenv-php
Enter fullscreen mode Exit fullscreen mode

That’s it! You’re ready to start managing your environment variables.


🧩 Usage

Here’s how simple it is to get started:

1. Create a .env file in your project root

APP_NAME=MyAwesomeApp
APP_ENV=local
APP_DEBUG=true
DB_HOST=127.0.0.1
DB_PORT=3306
DB_USER=root
DB_PASS=secret
Enter fullscreen mode Exit fullscreen mode

2. Load environment variables in PHP

use CoreEnv\CoreEnv;

// Load the .env file
CoreEnv::load(__DIR__ . '/.env');

// Retrieve variables
$appName = CoreEnv::get('APP_NAME');
$dbHost  = CoreEnv::get('DB_HOST', 'localhost');
$debug   = CoreEnv::getBoolean('APP_DEBUG', false);
Enter fullscreen mode Exit fullscreen mode

3. Access variables globally

After loading, variables are available in:

$_ENV['APP_NAME'];
$_SERVER['APP_ENV'];
getenv('DB_HOST');
Enter fullscreen mode Exit fullscreen mode

🧠 Helper Methods

Method Description
CoreEnv::get($key, $default = null) Retrieve any variable with an optional default
CoreEnv::getBoolean($key, $default = false) Automatically cast truthy/falsy values
CoreEnv::getInt($key, $default = 0) Retrieve and cast numeric values
CoreEnv::required($keys = []) Ensure required variables exist, else throw an exception

Example:

CoreEnv::required(['DB_HOST', 'DB_USER', 'DB_PASS']);
Enter fullscreen mode Exit fullscreen mode

🔒 Environment File Example

Here’s an example .env file for a database configuration:

DB_HOST=localhost
DB_PORT=3306
DB_NAME=mydatabase
DB_USER=root
DB_PASS=supersecret
DEBUG=true
Enter fullscreen mode Exit fullscreen mode

With CoreEnv:

$dbName = CoreEnv::get('DB_NAME');
$debug  = CoreEnv::getBoolean('DEBUG');
Enter fullscreen mode Exit fullscreen mode

🧹 Pro Tips

  • You can load multiple .env files if needed:
  CoreEnv::load(__DIR__ . '/.env');
  CoreEnv::load(__DIR__ . '/.env.local');
Enter fullscreen mode Exit fullscreen mode
  • Variables loaded later override earlier ones.
  • Always add .env to your .gitignore file to keep secrets safe.

💡 Why Use CoreEnv?

While frameworks like Laravel already include environment helpers, CoreEnv is framework-independent, making it ideal for any PHP project.
It’s perfect for developers who want:

  • A clean, simple .env management tool
  • Zero dependency solution
  • Works seamlessly in any PHP environment

📦 Repository & Source Code

You can explore the full source code and contribute here:
👉 https://github.com/sachin-sanchania/coreenv-php


❤️ Contributing

Contributions are welcome!
Feel free to fork the repository, open issues, or submit pull requests to help improve CoreEnv.


🧾 License

CoreEnv is open-source software licensed under the MIT License.


Thanks for reading!
If you find this project useful, don’t forget to ⭐ star the repo on GitHub — it helps a lot.
Follow me on Dev.to for more PHP libraries, Laravel packages, and open-source updates.

Top comments (2)

Collapse
 
xwero profile image
david duymelinck

The frameworks also use a framework agnostic .env library. Symfony uses Symfony Dotenv and Laravel uses PHP dotenv.

While PHP dotenv does have dependencies, it is the library with the most functionality.

I applaud your effort, but with the recent supply chain attacks I think people are going to prefer the established libraries.

Collapse
 
sachinsanchania profile image
Sachin Sanchania

You’re absolutely right. - libraries like PHP dotenv and Symfony Dotenv are excellent and well-established.

My goal with CoreEnv isn’t to compete with those established libraries, but rather to provide a lightweight, dependency-free, and framework-agnostic alternative for developers who need something minimal - especially for standalone PHP projects, microservices, or internal scripts where pulling in larger dependencies isn’t ideal.
I appreciate your insight - it’s great to have this kind of discussion! 🙏