> For the complete documentation index, see [llms.txt](https://quantumphp.gitbook.io/docs/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://quantumphp.gitbook.io/docs/advanced-features/environment.md).

# Environment

The Quantum PHP Framework uses an environment-based configuration system to manage settings for different stages of the application lifecycle (e.g., development, production, testing).

## File Selection

The framework selects the environment file based on the `app_env` setting, which is determined during the initial loading phase.

* **Production**: Uses `.env`.
* **Other environments**: Uses `.env.{app_env}` (e.g., `.env.development`, `.env.staging`).

## Lifecycle and Loading

The `Environment` service implements a "one-shot" loading pattern.

### `load(Setup $setup)`

* **One-shot**: Once the environment is loaded, subsequent calls to `load()` are no-ops.
* **Initialization**: It identifies the target `.env` file, validates its existence, loads the variables, and marks the environment as loaded.

### `env(string $key, $default = null)`

This helper function provides access to environment variables.

* **Contract**: You must call `load()` before accessing any variables via `env()`. If the environment has not been loaded yet, calling `env()` will throw an `EnvException`.

> **Warning**: In long-lived processes (like workers, daemons, or some test suites), be aware that once `load()` is executed, the environment state is fixed. Reloading variables requires the process or the service state to be reset.

## Server Accessor Contract

The `Server` class provides a controlled gateway to the server state.

### Metadata Accessor Details

| Method              | Behavior Contract                                                                                                                          |
| ------------------- | ------------------------------------------------------------------------------------------------------------------------------------------ |
| `protocol()`        | Returns `https` if `HTTPS` is truthy/not `off` OR `SERVER_PORT` is `443`; defaults to `http`.                                              |
| `contentType(true)` | Returns `CONTENT_TYPE` but strips all characters after the first `;` (e.g., `application/json; charset=UTF-8` becomes `application/json`). |
| `getAllHeaders()`   | Normalizes `HTTP_*` keys by removing the prefix, converting underscores to hyphens, and forcing lowercase.                                 |
| `acceptedLang()`    | Extracts the first language from `HTTP_ACCEPT_LANGUAGE` and returns only the first two characters in lowercase.                            |
| `ip()`              | Precedence: `HTTP_CLIENT_IP` -> `HTTP_X_FORWARDED_FOR` -> `REMOTE_ADDR`.                                                                   |

> **Proxy/Security Caveat**: The `ip()` method trusts headers provided by the client (proxies/load balancers). In production environments, ensure your entry point configures the application to support only trusted proxies.

```php
$setup = new Setup('config', 'env', true, 'php');

$env = Di::get(Environment::class);
$env->load($setup);

// Access a variable
$dbHost = env('DB_HOST', 'localhost');
```

## What to read next

After environment loading behavior, continue with:

* [Configuration](/docs/advanced-features/configuration.md)
* [Request Lifecycle](/docs/advanced-features/request-lifecycle.md)
* [Modules](/docs/core-concepts/modules.md)


---

# Agent Instructions
This documentation is published with GitBook. GitBook is the documentation platform designed so that both humans and AI agents can read, navigate, and reason over technical content effectively. Learn more at gitbook.com.

## Querying This Documentation
If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter, and the optional `goal` query parameter:

```
GET https://quantumphp.gitbook.io/docs/advanced-features/environment.md?ask=<question>&goal=<endgoal>
```

`ask` is the immediate question: it should be specific, self-contained, and written in natural language.
`goal` is optional and describes the broader end goal you are ultimately trying to accomplish on behalf of the user. GitBook uses it to tailor the answer towards what is most useful for that goal.

The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
