PHP Lesson 17 – Environmental Variables

Last updated on December 10th, 2023

What is environmental variable in PHP?

In PHP, an environmental variable is like a special container that holds important information about the computer or server where your PHP code is running. It can store things like the name of the website you’re visiting, the type of web browser you’re using, or even your computer’s username.

For example, if you want to know the name of the web server that’s running your PHP code, you can use a special word like $_SERVER[‘SERVER_NAME’] to find out.

These variables are helpful because they let your PHP code know more about the place where it’s working. This way, your code can change its behavior based on where it’s running. This is really useful because it allows your PHP programs to work correctly no matter where they’re being used, without you having to change the code each time.

List of environmental variables in PHP

$_SERVER
It holds information about headers, paths, and script locations. For example, $_SERVER[‘SERVER_NAME’] gives you the name of the server.

$_ENV
It stores information about the environment, like system settings. You can use it to access details about the system you’re using.

$_GET and $_POST
These store data sent to the script via a web form. For example, if you have a form with a text box, the text you type in that box is accessible through $_POST or $_GET depending on the form’s method.

$_COOKIE
It holds any cookies sent to the script. Cookies are small pieces of data stored on the client’s computer. For example, it can hold information about the user’s preferences on a website.

$_SESSION
It stores data across multiple pages. For instance, you can use it to keep track of whether a user is logged in or not.

Examples of using environmental variables in PHP

Accessing the Server Name with $_SERVER:

<?php
// Getting the server name and displaying it
$serverName = $_SERVER['SERVER_NAME'];
echo "You are on the server: $serverName";
?>

This script uses the $_SERVER variable to get the name of the server where the PHP script is running. It then prints out a message containing the server name.

Using $_ENV to Access Environment Variables:

<?php
// Accessing an environment variable using $_ENV
$userHome = $_ENV['HOME'];
echo "Your home directory is: $userHome";
?>

This code accesses an environmental variable HOME, which typically stores the home directory of the current user. It then displays the home directory on the webpage.

Using $_GET to Retrieve Data from a Form:

HTML Code:

<html>
<body>

<form method="get" action="process.php">
  Name: <input type="text" name="name">
  <input type="submit">
</form>

</body>
</html>

PHP Code:

<?php
// Retrieving data from the form using $_GET
$name = $_GET['name'];
echo "Hello, $name!";
?>

This set of code consists of an HTML form that takes input from the user. The form data is then processed in the PHP script process.php using the $_GET variable. The script retrieves the value entered in the “Name” text field and displays a greeting message using that name.

In this case you need to create 2 files. Open Notepad or Notepad++, put the HTML code from the above example into it and save it as HTML page test.html. Then open Notepad again, put the PHP code from the above example into it and save it as a PHP page process.php. Now put both pages in xampp’s htdocs folder. Start the Apache server through the XAMPP control panel. Open the test.html page through the address localhost/test.html, write something in the text field and click the Submit button. The result will be like this:

GET example

Using $_SESSION to Track User Login:

<?php
// Starting a session and storing a value in $_SESSION
session_start();
$_SESSION['username'] = 'JohnDoe';
echo "Session username set.";
?>

This script starts a session using session_start() and stores the value “JohnDoe” in the $_SESSION variable. The script then prints a message confirming that the session username has been set.