start learning
Image 1

Superglobal $_ENV in PHP

In PHP, $_ENV is a superglobal variable that provides access to the environment variables defined on the server where PHP is running. Environment variables are external variables set at the operating system or server level and can store information like system paths, configuration values, or custom data. $_ENV allows PHP scripts to read and utilize these environment variables.

Example with Clarification :

Let's create an example to demonstrate how to use the $_ENV superglobal to access environment variables in PHP.
First, let's set an environment variable on your server. This can vary depending on your server setup.

In Unix-based systems, you can set environment variables in the terminal :
export MY_CUSTOM_VAR="Hello, World!"

Or in a Windows command prompt :
set MY_CUSTOM_VAR="Hello, World!"

Now, create a PHP script to access this environment variable :
<!DOCTYPE html>
    <title>PHP $_ENV Superglobal Example</title>
    <h1>Environment Variable Example</h1>
    // Check if the environment variable exists
    if (isset($_ENV['MY_CUSTOM_VAR'])) {
        $customVar = $_ENV['MY_CUSTOM_VAR'];
        echo "<p>Environment Variable Value: $customVar</p>";
    } else {
        echo "<p>Environment Variable not found.</p>";

  1. In this example, we have set an environment variable called MY_CUSTOM_VAR with the value "Hello, World!" on the server where PHP is running.
  2. The PHP script checks for the existence of this environment variable using the isset function and $_ENV['MY_CUSTOM_VAR'].
  3. If the environment variable exists, it is retrieved, and its value is displayed in the HTML.
  4. If the environment variable is not found, a message is displayed indicating that the variable is not present.

This example demonstrates how to use the $_ENV superglobal to access environment variables. It is useful when you need to read server-related information, custom configurations, or any other external data stored as environment variables. Keep in mind that the availability and naming of environment variables may vary depending on the server and its configuration.

Environment variables are often set at the server or system level, and you would typically need root or administrative access to do so. In a shared hosting environment with cPanel, you might not have this level of access.
If you need to set environment variables for a PHP application on your cPanel-hosted website, you might consider alternative methods such as :

  1. Using .htaccess: You can set environment variables in the .htaccess file if your web server is Apache. This is done using the SetEnv directive. For example:
    SetEnv MY_CUSTOM_VAR "Hello, World!"
  2. Using PHP Configuration: You can set environment variables in your PHP code itself, using putenv or $_ENV. For example:
    putenv("MY_CUSTOM_VAR=Hello, World!");

Please note that in a shared hosting environment, you might have limited control over server-level settings, and the ability to set environment variables may depend on your hosting provider's policies and configurations. If you need to set specific environment variables, you should check with your hosting provider or consider upgrading to a VPS (Virtual Private Server) or dedicated server where you have more control over the server environment.