Git Product home page Git Product logo

dotenv's Introduction

InitPHP DotENV

Loads environment variables from .env or .env.php file.

Latest Stable Version Total Downloads Latest Unstable Version License PHP Version Require

Requirements

  • PHP 5.6 or higher

Installation

composer require initphp/dotenv

Usage

Note : Lines starting with any non-alphanumeric character are counted as comments and are not processed.

Note : Existing definitions in the $_SERVER or $_ENV globals are not processed.

.env File

Note that .env files are externally accessible. To prevent access with .htaccess or better yet keep your .env file in a directory that cannot be accessed externally.

/home/www/.env :

# Comment Line
SITE_URL = http://lvh.me

PAGE_URL = ${SITE_URL}/page

; Comment Line
TRUE_VALE = true

EMPTY_VALUE = empty

FALSE_VALUE = false

NULL_VALUE = null

NUMERIC_VALUE = 13
PI_NUMBER = 3.14

any.php :

require_once "vendor/autoload.php";
use \InitPHP\DotENV\DotENV;

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

DotENV::get('TRUE_VALE'); // true
DotENV::get('FALSE_VALUE'); // false
DotENV::get('SITE_URL'); // "http://lvh.me"
DotENV::get('PAGE_URL'); // "http://lvh.me/page"
DotENV::get('EMPTY_VALUE'); // ""
DotENV::get('NULL_VALUE'); // NULL
DotENV::get('NUMERIC_VALUE'); // 13
DotENV::get('PI_NUMBER'); // 3.14

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

.env.php

/home/www/.env.php :

<?php 
return [
    'SITE_URL'      => 'http://lvh.me',
    'PAGE_URL'      => '${SITE_URL}/page',
    'TRUE_VALE'     => true,
    'EMPTY_VALUE'   => '',
    'FALSE_VALUE'   => false,
    'NULL_VALUE'    => null,
    'NUMERIC_VALUE' => 13
];

any.php :

require_once "vendor/autoload.php";
use \InitPHP\DotENV\DotENV;

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


DotENV::get('TRUE_VALE'); // true
DotENV::get('FALSE_VALUE'); // false
DotENV::get('SITE_URL'); // "http://lvh.me"
DotENV::get('EMPTY_VALUE'); // ""
DotENV::get('NULL_VALUE'); // NULL
DotENV::get('NUMERIC_VALUE'); // 13

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

DotENV::create()

Reads and defines an .env or .env.php file.

public function create(string $path, bool $debug = true): void;
  • $path : The path to the file to be uploaded. If you define a directory path, Dotenv will try to search for the .env or .env.php file itself.
  • $debug : Defines the exception throwing state. If false no exception is thrown.

Note : If the file is not found, the file is not a .env/.env.php file, or is unreadable, it throws a \Exception variant.

DotENV::get()

Returns an ENV value.

public function get(string $name, mixed $default = null): mixed;

Note : The priority order is as follows;

$_ENV -> $_SERVER -> getenv()

DotENV::env()

It's an alias for the Dotenv::get() method.

public function env(string $name, mixed $default = null): mixed;

Credits

License

Copyright © 2022 MIT License

dotenv's People

Contributors

muhammetsafak avatar

Stargazers

 avatar

Watchers

 avatar

Forkers

deawx

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.