PHP code example of initphp / dotenv

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

    

initphp / dotenv example snippets




use InitPHP\DotENV\DotENV;

DotENV::create('/home/www/.env');     // or DotENV::create('/home/www');

DotENV::get('SITE_URL');      // "http://lvh.me"
DotENV::get('PAGE_URL');      // "http://lvh.me/page"
DotENV::get('TRUE_VALUE');    // true       (bool)
DotENV::get('FALSE_VALUE');   // false      (bool)
DotENV::get('NULL_VALUE');    // null
DotENV::get('EMPTY_VALUE');   // ""         (string)
DotENV::get('NUMERIC_VALUE'); // 13         (int)
DotENV::get('PI_NUMBER');     // 3.14       (float)
DotENV::get('ZIP_CODE');      // "007"      (string — leading zero preserved)

DotENV::get('NOT_FOUND', 'hi'); // "hi"

env('SITE_URL');              // global helper, same shared state

use InitPHP\DotENV\Repository;

$env = new Repository();
$env->create('/home/www/.env');
$env->get('SITE_URL');

use InitPHP\DotENV\DotENV;

DotENV::create('/home/www/.env');

// Reference = a checked-in example file.
$report = DotENV::drift('/home/www/.env.example');

// Reference = an explicit ();        // ['APP_KEY']
    $report->toArray();           // ['missing' => [...], 'extra' => [...], 'empty' => [...]]
}

// Also flag undocumented and blank-but-.example', ['extra' => true, 'empty' => true]);

use InitPHP\DotENV\Exceptions\DriftException;

try {
    DotENV::assertNoDrift('/home/www/.env.example');
} catch (DriftException $e) {
    fwrite(STDERR, $e->getMessage() . PHP_EOL);
    fwrite(STDERR, implode(', ', $e->getReport()->getMissing()) . PHP_EOL);
    exit(1);
}
dotenv
# Comment line
SITE_URL = http://lvh.me
PAGE_URL = ${SITE_URL}/page

; Another comment
TRUE_VALUE  = true
FALSE_VALUE = false
NULL_VALUE  = null
EMPTY_VALUE = empty

NUMERIC_VALUE = 13
PI_NUMBER     = 3.14
ZIP_CODE      = 007