Using environment variables in PHP with .env

Veröffentlicht am: 21 Mai 2025
auf dem Kanal: 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.


Auf dieser Seite können Sie das Online-Video Using environment variables in PHP with .env mit der Dauer stunde minuten sekunde in guter Qualität ansehen, das der Benutzer PHP Explained 21 Mai 2025 hochgeladen hat, den Link mit Freunden und Bekannten teilen, dieses Video wurde auf Youtube bereits 187 Mal angesehen und es wurde von 0 den Zuschauern gefallen. Viel Spaß beim Betrachtenden Zuschauern gefallen!