getenv

getenv(?string $name = null, bool $local_only = false): string|array|false

Reads process environment variables: by name (false when absent; omit the name for the whole environment array); values written by putenv are immediately visible to later reads in the same process.

C C23

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

int main(void) {
    /* C: getenv(3) reads the environment; POSIX setenv writes it (this process only) */
    setenv("CODEABC_DEMO_GREETING", "hello from setenv", 1);
    printf("%s\n", getenv("CODEABC_DEMO_GREETING"));
    printf("%s\n", getenv("CODEABC_VAR_THAT_CANNOT_EXIST") == NULL ? "absent name -> NULL" : "set");
    return 0;
}
Output
hello from setenv
absent name -> NULL

C++ C++26

C++26
#include <cstdlib>
#include <iostream>

int main() {
    // C++: std::getenv is the C API; POSIX setenv writes (this process only)
    setenv("CODEABC_DEMO_GREETING", "hello from setenv", 1);
    std::cout << std::getenv("CODEABC_DEMO_GREETING") << "\n";
    std::cout << (std::getenv("CODEABC_VAR_THAT_CANNOT_EXIST") == nullptr ? "absent name -> nullptr" : "set") << "\n";
    return 0;
}
Output
hello from setenv
absent name -> nullptr

C# v11.0.100

v11.0.100
// C#: GetEnvironmentVariable returns null for absent names; SetEnvironmentVariable writes (process scope)
Environment.SetEnvironmentVariable("CODEABC_DEMO_GREETING", "hello from SetEnvironmentVariable");
Console.WriteLine(Environment.GetEnvironmentVariable("CODEABC_DEMO_GREETING"));
Console.WriteLine(Environment.GetEnvironmentVariable("CODEABC_VAR_THAT_CANNOT_EXIST") ?? "absent name -> null");
Output
hello from SetEnvironmentVariable
absent name -> null

Go v1.27.4

v1.27.4
package main

import (
	"fmt"
	"os"
)

func main() {
	// Go: os.Getenv returns "" for missing names - LookupEnv tells absence apart
	os.Setenv("CODEABC_DEMO_GREETING", "hello from os.Setenv")
	v, ok := os.LookupEnv("CODEABC_DEMO_GREETING")
	fmt.Println(v)
	_, ok = os.LookupEnv("CODEABC_VAR_THAT_CANNOT_EXIST")
	fmt.Println("missing name seen:", ok)
}
Output
hello from os.Setenv
missing name seen: false

Java v27.0.1

v27.0.1
import java.util.Optional;

public class Main {
    public static void main(String[] args) {
        // Java: System.getenv reads a read-only snapshot; no setter exists at all
        String absent = System.getenv("CODEABC_VAR_THAT_CANNOT_EXIST");
        System.out.println(absent == null ? "absent name -> null" : absent);
        Optional<String> maybe = Optional.ofNullable(System.getenv("CODEABC_VAR_THAT_CANNOT_EXIST"));
        System.out.println(maybe.orElse("Optional stays empty for absent names"));
    }
}
Output
absent name -> null
Optional stays empty for absent names

JavaScript v26.13.1

v26.13.1
// Node: process.env is a plain object view over the environment
process.env.CODEABC_DEMO_GREETING = "hello from process.env";
console.log(process.env.CODEABC_DEMO_GREETING);
console.log(process.env.CODEABC_VAR_THAT_CANNOT_EXIST); // undefined when absent
Output
hello from process.env
undefined

PHP v8.6.0

v8.6.0
// getenv reads the process environment; a name set by the script itself is deterministic
putenv("CODEABC_DEMO_GREETING=hello from putenv");
echo getenv("CODEABC_DEMO_GREETING"), "\n";
var_dump(getenv("CODEABC_VAR_THAT_CANNOT_EXIST")); // false when absent
Output
hello from putenv
bool(false)

Python v3.15.2

v3.15.2
# Python: os.environ is a dict-like view; use .get for optional names
import os
os.environ["CODEABC_DEMO_GREETING"] = "hello from os.environ"
print(os.environ["CODEABC_DEMO_GREETING"])
print(os.environ.get("CODEABC_VAR_THAT_CANNOT_EXIST"))  # None when absent
Output
hello from os.environ
None

Rust v1.99.1

v1.99.1
fn main() {
    // Rust: std::env::var returns Result - Err(NotPresent) means the name is absent
    std::env::set_var("CODEABC_DEMO_GREETING", "hello from set_var");
    match std::env::var("CODEABC_DEMO_GREETING") {
        Ok(v) => println!("{}", v),
        Err(_) => println!("missing"),
    }
    match std::env::var("CODEABC_VAR_THAT_CANNOT_EXIST") {
        Ok(_) => println!("unexpected"),
        Err(_) => println!("missing name -> Err"),
    }
}
Output
hello from set_var
missing name -> Err

TypeScript v26.13.1

v26.13.1
// TS: process.env values are string|undefined - the absence is typed
process.env.CODEABC_DEMO_GREETING = "hello from process.env";
console.log(process.env.CODEABC_DEMO_GREETING);
console.log(process.env.CODEABC_VAR_THAT_CANNOT_EXIST); // undefined when absent
Output
hello from process.env
undefined

Language differences

C: getenv(3) returns char*; NULL when absent; POSIX setenv writes (see the putenv entry).

C++: std::getenv is the C API.

C#: GetEnvironmentVariable returns null when absent; SetEnvironmentVariable distinguishes Process/User/Machine targets.

Go: Getenv returns "" for missing names - it cannot tell "absent" from "empty"; use LookupEnv's second return value to distinguish.

Java: System.getenv is a read-only snapshot taken at startup and cannot be modified in-process (see the putenv entry).

JavaScript: process.env is a plain object view; absent names are undefined; assignment writes.

PHP: absent names return false - mind the difference from "" and "0"; calling without arguments returns the whole environment; under FastCGI an SAPI-set variable of the same name wins unless local_only=true restricts the lookup to this process (putenv) value; names are case-insensitive on Windows, sensitive on Linux.

Python: os.environ is a dict-like view; os.getenv returns None when absent; direct assignment os.environ["X"]="v" calls putenv internally.

Rust: std::env::var returns a Result; Err(NotPresent) means absent; since edition 2024 set_var/remove_var are unsafe (mutating the environment is risky with multiple threads).

TypeScript: same as JavaScript, typed string|undefined.