ZetCode

PHP gethostname Function

last modified April 4, 2025

The PHP gethostname function retrieves the standard host name for the local machine. It's useful for server identification and logging.

Basic Definition

gethostname returns the hostname of the machine where PHP is running. This is the same name returned by the hostname command in Unix.

Syntax: gethostname(): string|false. Returns the hostname as a string or false on failure. Requires no parameters.

Basic Hostname Retrieval

This example demonstrates the simplest usage of the gethostname function.

basic_hostname.php
<?php

declare(strict_types=1);

$hostname = gethostname();
echo "Server hostname: $hostname";

This code retrieves and displays the hostname of the current server. The hostname is typically set during system configuration.

Error Handling Example

This example shows how to properly handle potential failures.

error_handling.php
<?php

declare(strict_types=1);

$hostname = gethostname();

if ($hostname === false) {
    echo "Failed to retrieve hostname";
} else {
    echo "Hostname: $hostname";
}

Always check for false return value as the function might fail in rare cases. This ensures your application handles errors gracefully.

Using Hostname in Logging

This demonstrates using hostname in application logging for better debugging.

logging_example.php
<?php

declare(strict_types=1);

function logMessage(string $message): void {
    $hostname = gethostname();
    $timestamp = date('Y-m-d H:i:s');
    file_put_contents('app.log', "[$timestamp] [$hostname] $message\n", FILE_APPEND);
}

logMessage("Application started");

Including hostname in logs helps identify which server generated the log entry. This is especially useful in distributed systems.

Hostname-Based Configuration

This example shows how to load different configurations based on hostname.

hostname_config.php
<?php

declare(strict_types=1);

$hostname = gethostname();

switch ($hostname) {
    case 'production-server':
        $config = require 'config.prod.php';
        break;
    case 'staging-server':
        $config = require 'config.stage.php';
        break;
    default:
        $config = require 'config.dev.php';
}

This pattern allows different environments to automatically load appropriate configurations without manual intervention.

Comparing with $_SERVER Variables

This example compares gethostname with SERVER_NAME and HTTP_HOST variables.

server_vars.php
<?php

declare(strict_types=1);

echo "gethostname(): " . gethostname() . "\n";
echo "\$_SERVER['SERVER_NAME']: " . ($_SERVER['SERVER_NAME'] ?? 'N/A') . "\n";
echo "\$_SERVER['HTTP_HOST']: " . ($_SERVER['HTTP_HOST'] ?? 'N/A') . "\n";

gethostname returns the machine name while SERVER_NAME and HTTP_HOST return web server configuration values. These often differ in web environments.

Best Practices

Source

PHP gethostname Documentation

This tutorial covered the PHP gethostname function with practical examples for server identification and environment-specific logic.

Author

My name is Jan Bodnar, and I am a passionate programmer with extensive programming experience. I have been writing programming articles since 2007. To date, I have authored over 1,400 articles and 8 e-books. I possess more than ten years of experience in teaching programming.

List all PHP Network Functions.