Skip to content

Core\Env::all

Returns every environment variable as one array, with the variable's name as the key.

The array is sorted by name. Two calls give the same order, on every operating system. Every value is tainted: it comes from outside your program, so you must check or escape it before you use it in a query, a URL or a command. A variable whose name or value is not valid UTF-8 text is left out of the array. Core\Env::get with that name throws an error that says why.

In plain words: tainted means "not checked yet". You can compare the value or write it to a log. You cannot put it into a query or a command until you have checked it.

Good to know: a program cannot change its environment. There is no putenv.

Return value

Returns array<tainted string>. An array<string, tainted string>, sorted by name. Every call returns the same order, on every platform. A variable whose name or value is not valid UTF-8 is left out. get with that name throws an error for it.

Examples

The names come in order

<?nvs
// Shows that `Core\Env::all` returns the variables sorted by name.
var $all = Core\Env::all();
// This loop compares each name with the one before it.
// `Core\Str::compare` gives a number above 0 when the first name sorts after the second.
tainted string $previous = '';
int $outOfOrder = 0;
foreach ($all as tainted string $name => tainted string $value) {
if (Core\Str::compare($previous, $name) > 0) {
$outOfOrder = $outOfOrder + 1;
}
$previous = $name;
}
// The array is sorted, so this prints `0`.
echo 'names out of order: ', $outOfOrder, "\n";
Output
names out of order: 0

Defaults the environment can change

<?nvs
// Shows how to start from default settings and let the environment change some of them.
array<tainted string> $settings = [
'SHOP_PORT' => '8080',
'SHOP_LANGUAGE' => 'en',
];
// Every variable whose name starts with `SHOP_` replaces the default of the same name.
foreach (Core\Env::all() as tainted string $name => tainted string $value) {
if (Core\Str::startsWith($name, 'SHOP_')) {
$settings[$name] = $value;
}
}
// No variable on this computer starts with `SHOP_`, so this prints the defaults.
foreach ($settings as tainted string $name => tainted string $value) {
echo $name, ' = ', $value, "\n";
}
Output
SHOP_PORT = 8080
SHOP_LANGUAGE = en

Check the settings before the shop starts

<?nvs
// Shows how a program checks, when it starts, that every setting it needs is in the environment.
array<string> $required = ['SHOP_DB_HOST', 'SHOP_DB_NAME', 'SHOP_MAIL_FROM'];
// `Core\Arr::hasKey` checks whether the array has a key with that name.
// The names that are not there go into `$missing`.
var $all = Core\Env::all();
array<string> $missing = [];
foreach ($required as string $name) {
if (!Core\Arr::hasKey($all, $name)) {
$missing[] = $name;
}
}
// None of the three is set on this computer, so this prints all three names.
if (Core\Arr::count($missing) > 0) {
echo 'The shop cannot start. Missing: ', Core\Str::join($missing, ', '), "\n";
} else {
echo "All settings are present.\n";
}
Output
The shop cannot start. Missing: SHOP_DB_HOST, SHOP_DB_NAME, SHOP_MAIL_FROM