PHP code example of prinx / dotenv

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

    

prinx / dotenv example snippets


// Require composer autoload file if it has not been done yet.
ble not found.
 */
$hostname = env('DEV_DB_HOST');

/*
 * Retrieve an environment variable. Returns default value passed as second argument if variable not found
 */
$port = env('DEV_DB_PORT', 3306);

/*
 * Add a variable to the current loaded environment (will not save in the .env file)
 */
addenv('LOG_LEVEL', 'info');

/*
 * Wrtie variable to the env file. 
 * Will also automatically load the variable into the current environment.
 * If the file already contains the variable, the variable will be overwritten.
 */
persistenv('LOG_LEVEL', 'warn');
persistenv('LOG_LEVEL', 'debug');
persistenv('LOG_LEVEL', 'info');

/*
 * Get all environment variables
 */
env()
// OR
allenv();

// PHP
echo env('MESSAGE'); // App based on mysql database

// Require composer autoload file if it has not been done yet.
as usual
$apiKey = env('API_KEY');

$dotenv = dotenv();

$hostname = dotenv()->get('DEV_DB_HOST');

// With a default value
$hostname = dotenv()->get('DEV_DB_HOST', 'localhost');

$hostname = dotenv()->all();

// or use get without any parameter
$hostname = dotenv()->get();

dotenv()->add('GUEST_NAME', 'john');

dotenv()->persist('GUEST_NAME', 'john');

use Prinx\Dotenv;

$dotenv = new Dotenv('path/to/.env');

$dotenv->get('VARIABLE');
ini
VARIABLE_NAME=value
ini
# Supported: file|database
SESSION_DRIVER=file
ini
APP_DEBUG="true"

APP_DEBUG="false"
ini
# Will be got as a null
APP_DEBUG=null

APP_DEBUG="null"