Skip to content

Quick start

fkrzski edited this page Feb 1, 2022 · 2 revisions

Installation

Via composer CLI

composer require fkrzski/dotenv

Via composer.json

"require": {
    "fkrzski/dotenv": "^1.1",
},

Quick start

Quick start only for newest version (Currently 1.1)

Basic setup

For use this package you must include autoload.php file from composer /vendor directory

// file.php
require "./vendor/autoload.php";

Nextly you must use Dotenv class in your file.

// file.php
require "./vendor/autoload.php";

use fkrzski\Dotenv\Dotenv;

.env file and variables

First you need to create a .env file and put some variables in it

# .env file
FOO="bar"
BAR=foo
VAR=value

Then load variables from this file into your project

// file.php
require "./vendor/autoload.php";

use fkrzski\Dotenv\Dotenv;

$dotenv = new Dotenv('path\to\your\file\.env'); 
$dotenv->start();

echo getenv('FOO');   // Output: bar
echo $_SERVER['BAR']; // Output: foo
echo $_ENV['VAR'];    // Output: value

Taht's all!