The $_SERVER superglobal in PHP is an associative array containing information about headers, paths, and script locations. It provides details about the server environment, current request, and client data.
$_SERVER is always available in PHP scripts$_SERVER stores predefined indices that expose server and request-related metadata. These values are populated by the web server and PHP runtime.
// Display the server host name
<?php
echo "Server name: " . $_SERVER['SERVER_NAME'];
?>
// Show the current request URI
<?php
echo "Current URI: " . $_SERVER['REQUEST_URI'];
?>
// Fetch the client IP address
<?php
echo "Client IP address: " . $_SERVER['REMOTE_ADDR'];
?>
// Output browser and OS information
<?php
echo "User agent: " . $_SERVER['HTTP_USER_AGENT'];
?>
These examples output dynamic server values such as hostname, URL path, client IP address, and browser information depending on the request environment.
$_SERVER values dynamically instead of hardcoding URLs$_SERVER['SCRIPT_NAME']$_SERVER values using a loopHTTP_USER_AGENT