Why does getenv return 2 items in php-fpm, but many if I call $_SERVER first?

I am using Amazon Linux 2 with apache and php8.2-fpm. I have not tested this on any other linux distro.

If I run the following script via web browser

<?php
var_dump(getenv('PATH'));

it will output something as expected,

string(49) "/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin"

But if I try use getenv to output all environment variables I get just 2.

<?php
foreach (getenv() as $key => $value)
{
    echo "$key - $value<br />";
}

I will then get

USER - apache
HOME - /usr/share/httpd

but PATH is not here…

Oddly if I change the last example to have $_SERVER just accessed (and modifying $value to be strlen($value) but that is irrelevant to the question.

<?php
$_SERVER;
foreach (getenv() as $key => $value)
{
    echo $key.' - '.strlen($value).'<br />';
}

I now suddenly get ALL of this

USER - 6
HOME - 16
SCRIPT_NAME - 13
REQUEST_URI - 13
QUERY_STRING - 0
REQUEST_METHOD - 3
SERVER_PROTOCOL - 8
GATEWAY_INTERFACE - 7
REMOTE_PORT - 5
SCRIPT_FILENAME - 49
SERVER_ADMIN - 22
CONTEXT_DOCUMENT_ROOT - 36
CONTEXT_PREFIX - 0
REQUEST_SCHEME - 5
DOCUMENT_ROOT - 36
REMOTE_ADDR - 14
SERVER_PORT - 3
SERVER_ADDR - 14
SERVER_NAME - 20
SERVER_SOFTWARE - 6
SERVER_SIGNATURE - 0
PATH - 49
HTTP_HOST - 20
HTTP_COOKIE - 1575
HTTP_ACCEPT_LANGUAGE - 14
HTTP_ACCEPT_ENCODING - 23
HTTP_SEC_FETCH_DEST - 8
HTTP_SEC_FETCH_USER - 2
HTTP_SEC_FETCH_MODE - 8
HTTP_SEC_FETCH_SITE - 4
HTTP_ACCEPT - 135
HTTP_USER_AGENT - 131
HTTP_UPGRADE_INSECURE_REQUESTS - 1
HTTP_DNT - 1
HTTP_SEC_CH_UA_PLATFORM - 7
HTTP_SEC_CH_UA_MOBILE - 2
HTTP_SEC_CH_UA - 65
HTTP_CACHE_CONTROL - 9
proxy-nokeepalive - 1
H2_STREAM_TAG - 4
H2_STREAM_ID - 1
H2_PUSHED_ON - 0
H2_PUSHED - 0
H2_PUSH - 3
H2PUSH - 3
HTTP2 - 2
SSL_TLS_SNI - 20
HTTPS - 2
UNIQUE_ID - 27
FCGI_ROLE - 9
PHP_SELF - 13
REQUEST_TIME_FLOAT - 15
REQUEST_TIME - 10

What’s going on here? I can fetch PATH, but I can’t fetch everything until $_SERVER is touched.