Current Location: Home> Latest Articles> Comprehensive Guide to PHP filter_has_var() Function with Examples

Comprehensive Guide to PHP filter_has_var() Function with Examples

M66 2025-07-26

Introduction to filter_has_var() Function

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.

Function Syntax

<span class="fun">filter_has_var(type, var)</span>

Parameter Description

  • 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.

Return Value

The function returns true if the specified variable exists; otherwise, it returns false.

Example Code

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!");
   }
?>

Example Output

If the GET request does not include the “email” variable, the output will be:

<span class="fun">Email isn't there!</span>

Conclusion

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.