Summary: in this tutorial, you’ll learn about the filter_has_var()
function and how to use it to check if a variable exists in an input.
Introduction to the filter_has_var() function
The filter_has_var()
function checks if a variable of a specified type exists. Here’s the syntax of the filter_has_var()
function:
filter_has_var ( int $input_type , string $var_name ) : bool
Code language: Python (python)
The filter_has_var()
function has the following parameter:
$input_type
is the type of input that you want to search for a variable. The valid input types areINPUT_GET
,INPUT_POST
,INPUT_COOKIE
,INPUT_SERVER
, orINPUT_ENV
.$var_name
is the name of the variable to check.
The filter_has_var()
function returns true
if the $var_name
exists in the $input_type
or false
otherwise.
The PHP filter_has_var() function example
We’ll create a very simple form to demonstrate the filter_has_var()
function.
First, create the following files and directories:
.
├── css
│ └── style.css
├── inc
│ ├── get.php
│ ├── post.php
│ └── .htaccess
└── index.php
Code language: Python (python)
Second, add the following code to the index.php
file:
<?php
if ($_SERVER['REQUEST_METHOD'] === 'GET') {
require 'inc/get.php';
} else {
require 'inc/post.php';
}
Code language: Python (python)
If the request is GET, the index.php
file loads the form from the get.php
file. Otherwise, it loads the code for processing the form in the post.php
file.
Third, create a form in the get.php
file:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<link rel="stylesheet" href="css/style.css">
<title>PHP filter_has_var Demo</title>
</head>
<body>
<div class="container">
<form action="<?php echo htmlspecialchars($_SERVER['PHP_SELF']) ?>" method="post" class="shaddow">
<label for="name">Name:</label>
<input type="text" name="name" id="name">
<input type="submit" value="Submit">
</form>
</div>
</body>
</html>
Code language: HTML, XML (xml)
Finally, in the post.php
file, use the filter_has_var()
function to check if the form data contains the $name
variable:
<?php
// check the name variable in the post request
if (filter_has_var(INPUT_POST, 'name')) {
echo 'The name variable exists:' . htmlspecialchars($_POST['name']);
} else {
echo 'The name is required!';
}
Code language: HTML, XML (xml)
filter_has_var vs. isset
The isset()
function returns true
if a variable is declared and not null
. For example, the following checks if the name
variable in the $_POST
array:
<?php
if(isset($_POST['name'])) {
// process the name
}
Code language: Python (python)
In this example, the isset()
checks if the $_POST
variable has a key 'name'
and the $_POST['name']
is not null. However, the isset()
doesn’t check if the name
variable comes from the HTTP request or not. For example:
<?php
$_POST['email'] = '[email protected]';
if(isset($_POST['email'])) { // return true
// ...
}
Code language: Python (python)
In this example, we first manually set the $_POST['email']
to a value. And then we use the isset()
function to check if the email
variable exists. As a result, the isset()
function returns true
.
Unlike the isset()
function, the filter_has_var()
function doesn’t read the contents of the $_POST
array. It checks the variables in the request’s body. Therefore, the following example returns false
:
<?php
$_POST['email'] = '[email protected]';
if(filter_has_var(INPUT_POST, 'email')) { // return false
// ...
}
Code language: Python (python)
Summary
- Use the
filter_has_var()
function to check if a variable exists in a specified type, includingINPUT_POST
,INPUT_GET
,INPUT_COOKIE
,INPUT_SERVER
, orINPUT_ENV
.