PHP code example of rammewerk / environment

1. Go to this page and download the library: Download rammewerk/environment library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

rammewerk / environment example snippets


use Rammewerk\component\environment\src\Environment;

$env = new Environment();

// Load from environment variable file
$env->load( ROOT_DIR . '.env');

// Get value from environment
$debug_mode = $env->get( 'DEBUG_MODE' );

$env->load( ROOT_DIR . '.env');

# Warning: will overwrite value for keys that exist in both files.
$env->load( ROOT_DIR . '.env-app');

# You can also define new variables or overwrite values on the fly.
$env->set('NEW_KEY', 'new value');

# You can load files from wherever you want.
$env_file = ROOT_DIR . '/app/.env';

# You decide where to put the cache.
$cache_file = CACHE_DIR . 'env-cache.json';

# Load the environment variables
# If cache does not exist it will create one.
# If cache exist, and is newer than the env_file, it will load from cache.
$env->load( $env_file, $cache_file);

# You can reload the file at any time.
$env->load( $env_file, $cache_file);

# But you cannot define the same cache file path for a different env file.
# This will throw a \RuntimeException()
$env->load( $some_other_env_file, $cache_file);

# And, if you want to reload and build new cache for all previous loaded env-files, you can do so
$env->reload();

use Rammewerk\component\environment\src\Validator;

...

# Validate variables when loading from file.
# If loaded from cache - it will not run validation.
$env->load( $env_file, $cache_file, static function( Validator $env) {
    $env->

$env->getString('KEY1'); // Returns string or null
$env->getInt('KEY2'); // Returns int or null
$env->getFloat('KEY3'); // Returns float or null
$env->getBool('KEY4'); // Returns bool
$env->getArray('KEY5'); // Returns array or null