gethostname

gethostname(): string|false

Gets the local host name: CI and local machines necessarily differ, so the deterministic strategy prints only derived facts (success + non-empty), never the name itself.

C C23

C23
#include <stdio.h>
#include <unistd.h>

int main(void) {
    // POSIX gethostname(2) - the C original behind PHP's function
    char buf[256];
    int rc = gethostname(buf, sizeof buf);
    printf("ok: %s\n", rc == 0 ? "yes" : "no");
    printf("length > 0: %s\n", rc == 0 && buf[0] != '\0' ? "yes" : "no");
    return 0;
}
Output
ok: yes
length > 0: yes

C++ C++26

C++26
#include <cstdio>
#include <unistd.h>

int main() {
    // No standard C++ API - the POSIX gethostname is used directly
    char buf[256];
    int rc = gethostname(buf, sizeof buf);
    std::printf("ok: %s\n", rc == 0 ? "yes" : "no");
    std::printf("length > 0: %s\n", rc == 0 && buf[0] != '\0' ? "yes" : "no");
}
Output
ok: yes
length > 0: yes

C# v11.0.100

v11.0.100
// Environment.MachineName is the managed equivalent
string name = Environment.MachineName;
Console.WriteLine("ok: yes");
Console.WriteLine("length > 0: " + (name.Length > 0 ? "yes" : "no"));
Output
ok: yes
length > 0: yes

Go v1.27.4

v1.27.4
package main

import (
	"fmt"
	"os"
)

func main() {
	// os.Hostname() wraps the POSIX call
	name, err := os.Hostname()
	fmt.Println("ok:", err == nil)
	fmt.Println("length > 0:", len(name) > 0)
}
Output
ok: true
length > 0: true

Java v27.0.1

v27.0.1
import java.net.InetAddress;

public class Main {
    public static void main(String[] args) throws Exception {
        // InetAddress.getLocalHost().getHostName() is the classic way;
        // the value is machine-specific, so only derived facts are printed
        String name = InetAddress.getLocalHost().getHostName();
        System.out.println("ok: yes");
        System.out.println("length > 0: " + (name.length() > 0 ? "yes" : "no"));
    }
}
Output
ok: yes
length > 0: yes

JavaScript v26.13.1

v26.13.1
// os.hostname() is the native equivalent
const os = require("node:os");
const name = os.hostname();
console.log("ok: yes");
console.log("length > 0:", name.length > 0 ? "yes" : "no");
Output
ok: yes
length > 0: yes

PHP v8.6.0

v8.6.0
// gethostname() works under CLI too; the NAME itself varies by machine,
// so only derived facts are printed
$h = gethostname();
echo "ok: ", is_string($h) ? "yes" : "no", "\n";
echo "length > 0: ", is_string($h) && strlen($h) > 0 ? "yes" : "no", "\n";
Output
ok: yes
length > 0: yes

Python v3.15.2

v3.15.2
# socket.gethostname() wraps the POSIX call
import socket

name = socket.gethostname()
print("ok: yes")
print("length > 0:", "yes" if len(name) > 0 else "no")
Output
ok: yes
length > 0: yes

Rust v1.99.1

v1.99.1
fn main() {
    // Rust std has no hostname API; shell out to the hostname(1) command
    let out = std::process::Command::new("hostname").output().unwrap();
    let name = String::from_utf8_lossy(&out.stdout).trim().to_string();
    println!("ok: {}", if out.status.success() { "yes" } else { "no" });
    println!("length > 0: {}", if !name.is_empty() { "yes" } else { "no" });
}
Output
ok: yes
length > 0: yes

TypeScript v26.13.1

v26.13.1
// os.hostname() is the native equivalent
import * as os from "node:os";
const name: string = os.hostname();
console.log("ok: yes");
console.log("length > 0:", name.length > 0 ? "yes" : "no");
Output
ok: yes
length > 0: yes

Language differences

C: gethostname(2) is the POSIX original that PHP wraps.

C++: no standard API - use POSIX directly.

C#: Environment.MachineName is the managed equivalent.

Go: os.Hostname() returns (name, err).

Java: InetAddress.getLocalHost().getHostName() is classic but may involve name resolution underneath.

JavaScript: os.hostname(); browser JS has no such capability.

PHP: gethostname() works under both CLI and Web; php_uname("n") is a variant.

Python: socket.gethostname(); platform.node() is equivalent.

Rust: std has nothing; the third-party hostname crate or a command-line fallback fills in.

TypeScript: same as JavaScript.

Determinism note: the hostname necessarily varies by machine, so the sample only asserts environment-independent facts like success and non-empty.