PHP code example of arrilot / dotenv-php

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

    

arrilot / dotenv-php example snippets




return [
    'DB_USER' => 'root',
    'DB_PASSWORD' => 'secret',
];

 
use Arrilot\DotEnv\DotEnv;
DotEnv::load('/path/to/.env.php'); 

$dbUser = DotEnv::get('DB_USER');

$dbUser = DotEnv::get('DB_USER', 'admin');

function env($key, $default = null)
{
    return \Arrilot\DotEnv\DotEnv::get($key, $default);
}
...
$dbUser = env('DB_USER', 'admin');

$variables = DotEnv::all();

DotEnv::set('DB_USER', 'admin');
DotEnv::set('DB_PASSWORD', 'secret');
// or
DotEnv::set([
    'DB_USER'     => 'root',
    'DB_PASSWORD' => 'secret',
]);

DotEnv::load('/path/to/new/.env.php');
//or
DotEnv::load([
    'DB_USER'     => 'root',
    'DB_PASSWORD' => 'secret',
]);

DotEnv::setRequired(['DB_USER', 'DB_PASSWORD']);

DotEnv::copyVarsToPutenv($prefix = 'PHP_'); // putenv()
DotEnv::copyVarsToEnv(); // $_ENV
DotEnv::copyVarsToServer() // $_SERVER