PHP code example of adhocore / env

1. Go to this page and download the library: Download adhocore/env 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/ */

    

adhocore / env example snippets


use Ahc\Env\Loader;

// Load env variables from .env file to `putenv` by default:
(new Loader)->load('/project/root/.env');

// Pass in boolean second param to control if the env should be reloaded:
(new Loader)->load('/project/root/.env', true);

// Load to $_SERVER global:
(new Loader)->load('/project/root/.env', true, Loader::SERVER);

// Load to $_ENV global and putenv():
(new Loader)->load('/project/root/.env', true, Loader::ENV | Loader::PUTENV);

// Load to all targets:
(new Loader)->load('/project/root/.env', true, Loader::ALL);

use Ahc\Env\Retriever;

// Retrieve:
echo Retriever::getEnv($key);

// Default value:
echo Retriever::getEnv('PAYMENT_GATEWAY', 'stripe');

// Sanitization (pass third and optionally fourth parameters):
echo Retriever::getEnv('MYSQL_PORT', 3306, FILTER_VALIDATE_INT);

// Or you can use `env()` which is alias of `Retriever::getEnv()`:
echo env('THE_KEY');

if (!getenv('<LAST_ENV_APP_SHOULD_BE_AWARE_OF>')) {
    // Override false :)
    (new Loader)->load('/project/root/.env', false);
}