PHP gethostname() Function

Beginner
5 min read
Updated: Nov 2026
3 examples
Network

Definition

In PHP, gethostname() returns the host name of the local machine running the script. On failure it returns false.

This is handy in logs, health checks and multi-server apps when you want to know which machine handled a request. It does not perform a DNS query for a remote name.

Syntax

Call gethostname() with the values you need. Start with this line.

PHP
$name = gethostname();
CallWhat it does
gethostname()Returns the local host name string, or false on failure.
gethostname() ?: "unknown"Falls back to "unknown" when the call fails.
strtolower((string) gethostname())Forces a string and lowercases the local name for comparisons.

gethostname() takes no parameters. The value comes from the operating system, so it may be a short name or a fully qualified name depending on server configuration.

Library declarations
PHP
gethostname(): string|false

Parameters

ParameterDescription
—This function takes no parameters.

Return Value

Return ValueDescription
stringThe local machine host name.
falseReturned when the host name cannot be determined.

PHP Examples

Example 1 — Print the Local Host Name

Call gethostname() and print the result. The output below is a typical sample name.

PHP
<?php
$name = gethostname();

if ($name === false) {
    echo "Could not read host name", PHP_EOL;
} else {
    echo $name, PHP_EOL;
}

How It Works

  1. gethostname() asks the OS for the machine name.
  2. false means the name could not be read.
  3. Otherwise we print the string.
  4. "web-server-01" is sample output for the tutorial.

Example 2 — Safe Fallback for Logs

Build a log prefix that still works when gethostname() fails.

PHP
<?php
$host = gethostname();
$prefix = ($host === false) ? "host=unknown" : "host=" . $host;

echo $prefix, " event=startup", PHP_EOL;

How It Works

  1. Read the host name once into $host.
  2. Choose a fallback label when the value is false.
  3. Concatenate a simple log-style prefix.
  4. Append the event text after the host field.

Example 3 — Compare Against an Expected Name

Check whether this machine matches a configured server name.

PHP
<?php
$expected = "web-server-01";
$actual = gethostname();

if ($actual === $expected) {
    echo "this is the primary web node", PHP_EOL;
} else {
    echo "running on ", var_export($actual, true), PHP_EOL;
}

How It Works

  1. Store the name you expect in config.
  2. Read the real local name with gethostname().
  3. Strict comparison decides which branch runs.
  4. The success message assumes the sample name matched.

Frequently Asked Questions

It returns the local machine's host name as a string, or false if the name cannot be determined.
No. It reads the local system host name. Use gethostbyname() or dns_get_record() for DNS lookups.
Operating systems can return either a short name or a fully qualified domain name. That depends on server configuration, not on PHP alone.
Yes, when the system call fails. Always check before concatenating the value into logs or URLs.
They often show the same node name, but gethostname() is the dedicated helper for the local host name.

Next: getmxrr()

getmxrr() looks up MX (mail exchanger) records for a host name. It fills $hosts with the M.

getmxrr() tutorial →

About the author

Mari Selvan M P
Mari Selvan M P 🔗

Developer, cloud engineer, and technical writer

  • Experience 12 years building web and cloud systems
  • Focus Full Stack Development, AWS, and Developer Education

I write practical tutorials so students and working developers can learn by doing—from databases and APIs to deployment on AWS.

8 people found this page helpful