In PHP, filter_has_var() helps with runtime and application data. Checks whether a named input variable was supplied in the original GET, POST, COOKIE, SERVER or ENV input source.
What Is filter_has_var() in PHP?
The function or method has a specific purpose: Checks whether a named input variable was supplied in the original GET, POST, COOKIE, SERVER or ENV input source. The following examples show the main call, one additional situation, and an application-style use.
Syntax:
<?php
filter_has_var(int $input_type, string $var_name) : bool
Parameters and Return Value
$input_type(int) — argument used by this API to perform its operation; required.$var_name(string) — argument used by this API to perform its operation; required.
Example 1: Basic Usage
Checks whether a named input variable was supplied in the original GET, POST, COOKIE, SERVER or ENV input source.
<?php
var_dump(filter_has_var(INPUT_GET, "page"));
This example demonstrates the central operation. The final output depends on the supplied input and, where relevant, the runtime or connected service.
Example 2: Handling a Different Case
<?php
if (filter_has_var(INPUT_POST, "email")) { echo "Email field submitted"; }
The second example focuses on a different input or failure condition, so the behavior can be distinguished from the basic case.
Practical Example
<?php
if (!filter_has_var(INPUT_POST, "csrf_token")) { http_response_code(400); echo "Missing token"; }
The practical example demonstrates how the same subject fits into a larger application. Use your own file paths, endpoints and validated inputs rather than copying sample settings directly.
Important Behavior and Edge Cases
- Checks whether a named input variable was supplied in the original GET, POST, COOKIE, SERVER or ENV input source.
- Check the return value and exceptions for failure before using the result. If an extension or external system is required, the example only works when that dependency is available.
- Use the documented return type and handle false, null and exceptions according to the specific call.
Comparison With Related Functions
| Approach | Difference |
|---|---|
| filter_has_var | Checks whether a named input variable was supplied in the original GET, POST, COOKIE, SERVER or ENV input source. |
| filter_var | Validates or filters a supplied value directly. |
Common Beginner Mistakes
| Mistake | Correct Approach |
|---|---|
| Ignoring errors from filter_has_var | Check the actual result and handle the expected error path. |
| Using the demonstration unchanged in production | Adapt file paths, configuration, authentication and permission rules to the application. |
| Treating input or returned data as trusted | Apply validation at input boundaries and context-aware escaping at output boundaries. |
Key Takeaways
- Checks whether a named input variable was supplied in the original GET, POST, COOKIE, SERVER or ENV input source.
- Check the return value and exceptions for failure before using the result. If an extension or external system is required, the example only works when that dependency is available.
- Use the documented return type and handle false, null and exceptions according to the specific call.
Checks whether a named input variable was supplied in the original GET, POST, COOKIE, SERVER or ENV input source. Use this approach when its capabilities and constraints match the application. Handle invalid data, failure conditions and resource cleanup explicitly.