The filter_has_var() function is used to check if a specific type of input variable exists, making it one of the commonly used input validation functions in PHP.
<span class="fun">filter_has_var(type, var)</span>
type — The input type to check, with five possible values: INPUT_GET, INPUT_POST, INPUT_COOKIE, INPUT_SERVER, or INPUT_ENV.
var — The name of the variable to check, as a string.
The function returns true if the specified variable exists; otherwise, it returns false.
The following example demonstrates how to check if a variable named “email” is passed via GET method:
<?php
if (!filter_has_var(INPUT_GET, "email")) {
echo("Email isn't there!");
} else {
echo("Email is there!");
}
?>
If the GET request does not include the “email” variable, the output will be:
<span class="fun">Email isn't there!</span>
The filter_has_var() function is a useful tool in PHP for quickly detecting whether a specified variable exists in the request. It is suitable for input validation and security control scenarios. Mastering this function helps developers handle user input more effectively and improve code robustness.