Skip to content

Core\Env::get

Returns the value of one environment variable, or null if nothing set it.

The 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. The name must be text your program wrote itself. A name that came from a request does not compile, because a visitor could then read every variable, one request at a time. An empty value is still a value, so null means only one thing: the variable is not set.

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: if the value is not valid UTF-8 text, get throws a RuntimeError.

Parameters

  • $namestringThe variable's name, with the same case as in the environment. It may not be tainted. A request that could choose the name could read every variable, one at a time.

Return value

Returns ?tainted string. The value as a tainted string, or null if the variable is not set. The value is tainted because it comes from outside the program. Clean it before you use it in SQL, HTML or a shell command.

Errors

  • RuntimeErrorThe variable contains bytes that are not valid UTF-8, so its value is not a string. The method does not return null here, because null means the variable is not set. It does not repair the bytes either.

Examples

A setting with a default

<?nvs
// Shows how to read one setting and use a default when it is not set.
// `Core\Env::get` returns `null` when nothing set `SHOP_LANGUAGE`.
// The `??` operator then gives the default, "en".
var $language = Core\Env::get('SHOP_LANGUAGE') ?? 'en';
// No variable `SHOP_LANGUAGE` is set on this computer, so this prints `en`.
echo 'language: ', $language, "\n";
Output
language: en

Read a number from the environment

<?nvs
// Shows how to read a number from the environment and check it before you use it.
// The value is text. `as ?int` converts it to a whole number.
// If that is not possible, the result is `null`.
?int $port = (Core\Env::get('SHOP_PORT') ?? '8080') as ?int;
// `SHOP_PORT` is not set here, so the default "8080" is used and this prints `8080`.
if ($port == null) {
echo "SHOP_PORT must be a whole number\n";
} else {
echo 'port: ', $port, "\n";
}
Output
port: 8080

Stop when a secret is missing

<?nvs
// Shows a function that reads a setting the program cannot run without.
class Settings
{
// Returns the value of `$name`, or throws an error when the variable is not set.
public static function required(string $name): tainted string
{
var $value = Core\Env::get($name);
if ($value == null) {
throw new RuntimeError("{$name} is not set");
}
return $value;
}
}
// `SHOP_DB_PASSWORD` is not set on this computer, so this prints the error message.
try {
var $password = Settings::required('SHOP_DB_PASSWORD');
echo "connecting to the database\n";
} catch (RuntimeError $error) {
echo 'The shop cannot start: ', $error->message, "\n";
}
Output
The shop cannot start: SHOP_DB_PASSWORD is not set