Using environment variables in PHP with .env

Publicado em: 21 Maio 2025
no canal de: PHP Explained
187
0

The internet is a more complex than it was before. It is always good to safe the web sites and web applications from hackers. The .env file plays a remarkable role in this situation.

A web application file that starts with a dot like .env is not accessible by users. This file is used to secure sensitive data of web applications from outside world.

The .env file is created in the root folder of the project. This is a plain text file. This file defines the environment variables required for an application in key and value pairs.

Suppose we are storing the application name in the .env file.
APP_NAME = Project1

Now, we are retrieving this application name and show in our index.php file.
require_once realpath(__DIR__ . "/vendor/autoload.php");
use Dotenv\Dotenv;

$dotenv = Dotenv::createImmutable(__DIR__);
$dotenv-gt;load();

$app_name = $_ENV['APP_NAME'];
echo $app_name;

In this way we can create all the variables which are required in our project environment and access in our project files.


Nesta página do site você pode assistir ao vídeo on-line Using environment variables in PHP with .env duração hora minuto segundo em boa qualidade , que foi baixado pelo usuário PHP Explained 21 Maio 2025, compartilhe o link com seus amigos e conhecidos, no youtube este vídeo já foi visto 187 vezes e gostou 0 espectadores. Boa visualização!