Table of Content
User input does not always come through as expected. Sometimes values are missing or not set at all. This can lead to errors when your code tries to use that data. The filter_has_var() function helps avoid this. It checks if the input exists before your code uses it.
In this article you will learn how filter_has_var function works and its syntax. Also, you will see well-explained examples. Let’s get started.
What filter_has_var does
filter_has_var is a PHP function that checks if a variable is present in a specified input stream, such as $_GET, $_POST, or $_COOKIE. It returns true if the variable exists in the input stream and false if it does not.
This function helps you check if the input is available before you use it. Here is its syntax:
filter_has_var(int $type, string $variable_name);So, the $type (int) tells PHP where to look for the input. It must be one of these constants:
INPUT_GET: Looks in the$_GETarray (query string).INPUT_POST: Looks in the$_POSTarray (form data).INPUT_COOKIE: Looks in the$_COOKIEarray.INPUT_SERVER: Looks in the$_SERVERarray.INPUT_ENV: Looks in the$_ENVarray.
While $variable_name (string) refers to the name of the variable you want to check. For example, "username" or "email".
Here is an example:
if (filter_has_var(INPUT_GET, 'username')) {
echo "Username exists in the URL parameters!";
}This checks if the "username" variable is present in the URL query string. If it exists, the message will display. If not, nothing happens.
filter_has_var() is useful because it lets you check if a variable exists in the user input before you use it. This prevents errors like “undefined index” notices when a variable is missing.
It also helps you avoid using unexpected or invalid input. It is especially helpful when you handle form data or URL parameters.
Examples of Different Inputs with filter_has_var
The filter_has_var() allows you check if a variable exists in different input types such as:
GETPOSTCOOKIE.
This helps you handle user input and avoid errors across different request methods.
Use INPUT_GET when you need to check if a variable exists in the URL query string. This is common for values passed through links, like search terms or page numbers.
For example:
if (filter_has_var(INPUT_GET, 'id')) {
echo "ID exists in the URL.";
}The script checks if the id parameter exists in the URL, such as in example.com?page=2&id=123.
While the INPUT_POST checks for variables in the body of a POST request, like data submitted through a form.
Here is an example:
if (filter_has_var(INPUT_POST, 'email')) {
echo "Email exists in the form submission.";
}Here, the script checks if the email parameter is part of a form submission via POST.
You can also use the INPUT_COOKIE if you need to check if a variable exists in a cookie.
Here is an example:
if (filter_has_var(INPUT_COOKIE, 'user_session')) {
echo "User session cookie exists.";
}This example checks if a cookie named user_session exists.
You can also check for server variables (e.g., $_SERVER), which provide information about the environment or the current request.
if (filter_has_var(INPUT_SERVER, 'HTTP_USER_AGENT')) {
echo "User-Agent header exists.";
}This checks if the User-Agent header is set in the server request.
Wrapping Up
In this article, you learned how to use filter_has_var() to check for variables in different input streams like $_GET, $_POST, $_COOKIE, and $_SERVER.
It helps you to prevent errors and improve security to confirm that the data exists before processing it.
Here is a quick recap:
filter_has_varchecks if a variable exists in a specified input stream.- It helps you prevent errors and improve code security.
- You can use it with various request methods like
GETandPOST, etc. - It combines
filter_has_varwithfilter_input()which helps you to make sure that input is both present and sanitized before processing it.
FAQ’s
What does filter_has_var do in PHP?
How do I check if a GET parameter exists using filter_has_var?
if (filter_has_var(INPUT_GET, 'id')) {
echo "ID exists in the URL.";
}
This ensures the id parameter is present before using it.
Can filter_has_var be used with form submissions?
if (filter_has_var(INPUT_POST, 'email')) {
echo "Email exists in the form submission.";
}
This prevents errors from undefined form inputs.
What is the difference between isset() and filter_has_var()?
Similar Reads
PHP 8.4 released the array_any to test if at least one element matches a condition. Understand the array_any Function in…
If you are working with PHP and MySQL, one of the first tasks you may need is to create a…
PHP array_diff_uassoc compares arrays by values and keys. You can also pass a custom function to compare keys in your…
The PHP object is an instance from the PHP class or the main data structure that is already been created…
There are situations where you only need a specific number of rows returned. This is where the "LIMIT" clause in…
The PHP array_key_exists function checks a given key in an array and tells if that key exists or not. Understand…
In regard to the use of constants in PHP, clear understanding of their definition and use could be critical in…
Developers use the array_count_values function in PHP to find how many times each value appears in an array. What is…
PHP array_find was released in PHP 8.4 to locate a value inside an array and returns the first match. Understand…
Sometimes, when you run a script, you may encounter an error because the directory you're trying to access doesn't exist.…