Php filter_var() is a function that is used to filter a given variable with a specified filter. To sanitize and validate the data such as email_id, IP address, etc., in Php, the filter_var() function is used (which contains the data). Validation in the text means whether the entered data is in the correct format or not. For example, in an email id of the person, whether the ‘@’ sign is present or not. In a phone number field, all the numbers or digits should be present. Sanitization means to sanitize the data entered or remove the illegal or unnecessary characters from it to prevent any future issues. For example, removing unnecessary symbols and characters from user email.
Start Your Free Software Development Course
Web development, programming languages, Software testing & others
Syntax:
Below given is the basic syntax of filter_var() function in Php:
filter_var(variable, filtername, options)
where,
Return Value:The above function returns the filtered value or false if the data/ variable does not get filtered.
In PHP, the filter_var() method accepts the above-explained various parameters and returns the validated/ sanitized data. Validation means checking the format of the data as specified by the programmer, and Sanitization means removing the unnecessary characters from the data to return the data as required by the programmer.
Let us understand the working of the filter_var() function in Php along with the examples:
Validating an Integer value using filter_var() function:
Code:
Output:
Explanation:
In the above code, the Integer value to be validated is stored in the variable ‘value’ and is then passed in the filter_var() method along with the ‘FILTER_VALIDATE_INT’ filter name to validate it. Finally, conditional operators if and else are applied to check the condition, and the respective output is printed on the console using the ‘echo.’
Validating the IP address of the computer device using the filter_var() function
Code:
Output:
Explanation:
In the above code, the IP address of the computer or any other network device is validated using the filter_var() method. The ip address that is to be validated is stored in the variable ‘ip.’ Since the IP address has its specific format ‘x.y.z.w,’ it is validated using the ‘FILTER_VALIDATE_IP’ in the filter_var() function. Finally, the ip address passed is validated, and the respective output is printed on the console using ‘echo.’
Sanitizing and Validating the URL address using the filter_var() function
Code:
Output:
Explanation:
In the above code, the URL address, which has a specific format, is sanitized first and then validated using the filter_var() method. The URL to be checked is stored in the variable ‘check_url.’ To sanitize the url, ‘FILTER_SANITIZE_URL’ is passed as a filter name along with the url. Once sanitized, url is then validated using the ‘FILTER_VALIDATE_URL’ filter name along with the url, and the respective output on validation is printed on the console using ‘echo.’
Validating the email address of the user using the filter_var() function
Code:
Output:
Explanation:
In the above example, the email address which is to be checked is stored in the variable ‘email_check.’ It is validated using the filter_var() function in Php, bypassing the email variable and the respective filter name (FILTER_VALIDATE_EMAIL). Since the passed email is invalid, so the response is printed on the console using the ‘echo.’
Code:
array("min_range" => 10,"max_range" => 4000)))) { echo "Integer $value is within the specified range"; } else { echo "Sorry!! Integer $value is not in the range provided by you"; } ?>
Output:
Explanation:
In the above example, the Integer value is to be validated for the given range, i.e., 10 to 400 is tested. Then, in the filter_var() function, the value to be tested is passed along with the filter name (FILTER_VALIDATE_INT) and 1 optional parameter, i.e., ‘options’ having the array with the minimum and maximum range specified. Finally, the variable is validated, and accordingly, the response is printed on the console using the ‘echo.’
The above description clearly explains what is filter_var() functions in Php and how it works to validate and sanitize the variable passed in it. It is one of the important functions that programmers commonly use to filter the data to prevent a security breach. However, this function facilitates the use of different filters by passing the different parameters according to the specific requirements, so the programmer needs to understand it deeply before using it in the program.
以上是PHP filter_var的詳細內容。更多資訊請關注PHP中文網其他相關文章!