Superglobals · Lesson 8 of 8
$_ENV
Read deployment configuration from environment variables.
Lesson
Environment variables let deployments configure secrets and service endpoints without hard-coding them in source. Availability through $_ENV depends on PHP configuration; getenv is another interface. Use a configuration library or framework, never commit real credentials, and provide safe defaults only for non-secret development settings.
Avoid printing environment values into logs or HTTP responses.
Further guidance
Superglobal values come from the request or process environment and can be missing or have an unexpected type. Check the key and shape before use, validate it for the operation, and avoid printing secrets or trusting client-controlled headers. Prefer the specific input source over merged request data.
Practice task: Create a fresh example for $_ENV. Predict the result before running it, then change one input and explain why the result changes. Test a normal case and one boundary or invalid case where it applies. Compare your output with the example and note one rule you would use in a real project.
Code examples
$_ENV
PHP
$mode = getenv('APP_ENV') ?: 'production';
echo $mode;
Expected output
The configured application environment
Key points
- Read deployment configuration from environment variables.
- Treat request and environment values as untrusted input.
- Check the expected type and presence before using a value.
Your learning progress
Finish this lesson
Optional practice
PHP playground
This site does not execute learner-provided PHP code. Connect a secured PHP sandbox service before enabling code execution.
Execution is disabled. No code is sent to a server.