PHP code example of wpscholar / phpdotenv

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

    

wpscholar / phpdotenv example snippets







$loader = new wpscholar\phpdotenv\Loader(); // Can also do wpscholar\phpdotenv\Loader::create() 
$loader
    ->config([ // Must be used to customize adapters, can also be used to set defaults or      // Uses $_ENV
            'global',   // Sets global variables
            'putenv',   // Uses putenv()
            'server'    // Uses $_SERVER
        ], 
        'defaults' => [
            'foo' => 'bar' // Set a default value if not provided in .env  	
        ],
        'd loads vars into memory.
    ->set( 'qux', $loader->get('foo') ); // Override variables after loading, but with access to existing variables before they are loaded into the environment.
    
// Validate variable values after parsing the .env file, but before loading the results into the environment.
$loader->validate('foo')->notEmpty();
$loader->validate('bar')->isBoolean();
$loader->validate('baz')->isInteger();
$loader->validate('qux')->notEmpty()->allowedValues( [ 'bar', 'baz' ] ); // Validations can be chained together.
$loader->validate('quux')->assert(function( $value ) { // Apply your own custom validation assertions.
    return is_int($value) && $value > 0 && $value <= 10;	
});

// Call load() to load variables into the environment without overwriting existing variables.
$loader->load();

// Call overload() to load variables into the environment, overwriting any existing variables.
$loader->overload();



$loader = wpscholar\phpdotenv\Loader::create();
$loader
    ->config([ 'adapters' => 'array'] ) // All values are self-contained in an array within the loader.
    ->aining the final values.

$bar = $loader->get('bar'); // Get a single value.



pscholar\phpdotenv\Loader;

$loader = new Loader();
$loader
	->config( [ 'adapters' => 'define' ] ) // Will only set PHP constants
	->R__ . '/wp',
		'DB_CHARSET'      => 'utf8',
		'DB_COLLATE'      => '',
		'DB_HOST'         => 'localhost',
		'WP_DEBUG'        => false,
		'WP_TABLE_PREFIX' => 'wp_',
	] )
	->parse( __DIR__ . '/.env' ) // Parse the .env file
	->set( 'WP_HOME', 'https://' . $_SERVER['HTTP_HOST'] )
	->set( 'WP_SITEURL', $loader->get( 'WP_HOME' ) . '/wp' ) // Use previously defined values to set other values.
	->set( 'WP_CONTENT_DIR', __DIR__ . '/content' )
	->set( 'WP_CONTENT_URL', $loader->get( 'WP_HOME' ) . '/content' )
	->set( 'DISALLOW_FILE_EDIT', true )
	->load(); // We could use overload() here, but we can't overwrite constants in PHP either way.

$table_prefix = WP_TABLE_PREFIX;