W3docs

PHP gethostname() Function: Everything You Need to Know

Learn how PHP gethostname() returns the host name of the machine running your script, what it returns on failure, and how it differs from php_uname().

The PHP gethostname() function returns the host name of the machine that is currently executing your PHP script — the same name you would see from the hostname command in a terminal. It is handy for logging which server handled a request, building per-host cache keys, or showing diagnostic information in an admin panel. This page covers its syntax, what it returns (including on failure), realistic use cases, common pitfalls, and how it relates to other host-lookup functions.

Syntax

gethostname() takes no parameters:

gethostname(): string|false

It returns a string containing the host name on success, or false on failure. The function was introduced in PHP 5.3 and replaces the older php_uname('n').

The host name is the machine name, not the value of the Host: HTTP header. On a web server, gethostname() returns the server's name (for example web-01), whereas the domain a visitor typed lives in $_SERVER['HTTP_HOST'].

Basic example

php— editable, runs on the server

The script asks the operating system for the local machine name and prints it. The exact output depends on where the code runs — it might be something like web-01, localhost, or a container ID such as a1b2c3d4e5f6.

Handling failure safely

Because gethostname() can return false, check the result before using it instead of assuming you always get a string. Use the strict === operator so an empty string is not mistaken for failure:

<?php

$hostname = gethostname();

if ($hostname === false) {
    echo "Could not determine the host name.";
} else {
    echo "Running on: $hostname";
}

When would I use it?

  • Logging and diagnostics — tag each log line with the server that produced it, which is invaluable behind a load balancer where many machines serve the same site.
  • Per-host cache or lock keys — prefix temporary files or cache entries so two servers do not clash.
  • Health and status pages — show operators which node they are looking at.
  • Distributed jobs — record which worker picked up a queued task.

gethostname() only answers "what is this machine called?". For looking up other hosts on the network, PHP provides separate functions:

A common follow-up is to combine them — get the local name, then resolve it to an IP:

<?php

$hostname = gethostname();
$ip = $hostname !== false ? gethostbyname($hostname) : 'unknown';

echo "Host: $hostname, resolved IP: $ip";

Common pitfalls

  • It is not the client's domain. Don't use gethostname() to detect which site/domain a request came in on; read $_SERVER['HTTP_HOST'] instead.
  • The value can be unexpected in containers. Inside Docker the host name is usually the (randomly generated) container ID, not your server's friendly name.
  • Always handle false. A misconfigured system can fail to report a host name; guard against it as shown above.

Conclusion

gethostname() is a small, parameterless function that returns the name of the machine running your script, or false on failure. Use it for logging, diagnostics, and per-host keys — and reach for gethostbyname(), gethostbyaddr(), or gethostbynamel() when you need to look up other hosts on the network.

Practice

Practice
What is the purpose of the gethostname() function in PHP?
What is the purpose of the gethostname() function in PHP?
Was this page helpful?