Download the PHP package datahihi1/tiny-env without Composer
On this page you can find all versions of the php package datahihi1/tiny-env. It is possible to download/install these versions without Composer. Possible dependencies are resolved automatically.
Download datahihi1/tiny-env
More information about datahihi1/tiny-env
Files in datahihi1/tiny-env
Package tiny-env
Short Description A simple environment variable loader for PHP applications
License MIT
Informations about the package tiny-env
TinyEnv
A simple environment variable loader for PHP applications. Designed for small projects, TinyEnv
minimizes resource usage while ensuring stable performance. It loads environment variables from .env
files and provides flexible methods to manage them.
Installation and Setup
Install via Composer
Installation is straightforward with Composer:
Or manually add it to your composer.json
:
Run composer install
or composer update
to download the package.
1. load()
- Basic Setup
After installing, include Composer's autoloader, create a TinyEnv
instance, and load the environment variables:
2. Fast Load Option
Use the fastLoad
option in the constructor to load variables immediately:
3. lazy()
- Lazy Load Option
Load only variables matching specific prefixes:
4. only()
- Load Only Specific Variables
Load only the specified environment variables by key (single or array):
Usage
TinyEnv
provides simple methods and helper functions to work with environment variables. Below are examples based on a sample .env
file:
1. env()
- Retrieve Environment Variables
Use the global env()
function to get environment variables:
2. setenv()
- Set or Update Variables
Use setenv()
to dynamically set or update environment variables. If file writing is enabled (default), it also updates the .env
file:
Note: To disable file writing, use TinyEnv::setAllowFileWrites(false)
.
3. validate_env()
- Validate Variables
Ensure variables meet specific rules (e.g., required
, int
, bool
, string
):
4. unload()
- Clear Variables
Remove all loaded environment variables from $_ENV
and the internal cache:
5. refresh()
- Reload Variables
Clear current variables and reload from the .env
file:
Example .env
File
Create a .env
file in your project root:
Full Example
Notes
- Ensure the
.env
file and its directory are readable/writable when usingsetenv()
. - Comments in
.env
files start with#
and are ignored. -
Use uppercase letters, numbers, and underscores for variable names (e.g.,
APP_KEY
).
Variable Interpolation
TinyEnv supports variable interpolation within .env
values. You can reference other variables using ${VAR_NAME}
syntax, and TinyEnv will automatically replace them with their corresponding values when loading:
In this example, DB_URL
will be set to localhost:3306
. Interpolation works recursively and supports any variable defined earlier in the file or already loaded into the environment.
Note: If a referenced variable is not defined, it will be replaced with an empty string.