PHP code example of laxity7 / dotenv

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

    

laxity7 / dotenv example snippets


$dotenv = new \Laxity7\DotEnv\DotEnv();
$dotenv->load(__DIR__ . '/.env');

$dotenv->get('APP_NAME');  // 'MyApp'
$dotenv->get('APP_DEBUG'); // true (boolean)
$dotenv->get('DB_PORT');   // 3306 (integer)
$dotenv->get('MISSING', 'default'); // 'default'

$_ENV['APP_NAME']; // 'MyApp'

$dotenv->load(__DIR__ . '/.env');
$dotenv->load(__DIR__ . '/.env.local');       // will not overwrite existing variables
$dotenv->load(__DIR__ . '/.env.local', true); // will overwrite existing variables

function env(string $name, $default = null) {
    static $dotenv = null;

    if ($dotenv === null) {
        $dotenv = new \Laxity7\DotEnv\DotEnv();
        $dotenv->load(__DIR__ . '/.env');
    }

    return $dotenv->get($name, $default);
}

$dotenv = new \Laxity7\DotEnv\DotEnv();
$dotenv->load(__DIR__ . '/.env');
\Laxity7\DotEnv\Env::load($dotenv);

// Now available anywhere:
\Laxity7\DotEnv\Env::get('APP_NAME');           // 'MyApp'
\Laxity7\DotEnv\Env::get('MISSING', 'default'); // 'default'
\Laxity7\DotEnv\Env::has('APP_NAME');            // true

$dotenv = new \Laxity7\DotEnv\DotEnv();
$dotenv->load(__DIR__ . '/.env');
\Laxity7\DotEnv\Env::load($dotenv);

function env(string $name, $default = null) {
    return \Laxity7\DotEnv\Env::get($name, $default);
}

$dotenv->get('APP_TITLE');    // 'MyApp (production)'
$dotenv->get('DATABASE_URL'); // 'mysql://localhost:3306/mydb'

$dotenv = new \Laxity7\DotEnv\DotEnv();

$dotenv->usePutEnv = true;
$dotenv->load(__DIR__ . '/.env');

\Laxity7\DotEnv\Env::load($dotenv); // initialize with a DotEnv instance
\Laxity7\DotEnv\Env::get('KEY', 'default');
\Laxity7\DotEnv\Env::has('KEY');

// laxity7/dotenv
$dotenv->get('FOO'); // true  (bool)
$dotenv->get('BAR'); // false (bool)
$dotenv->get('BAZ'); // 'false' (string — it's quoted)

// symfony/dotenv — all values are strings
$_ENV['FOO']; // 'true' (string)
dotenv
export APP_ENV=production
export APP_DEBUG=false