PHP filter_var

WBOY
發布: 2024-08-29 13:03:59
原創
924 人瀏覽過

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,

  • variable:This parameter stands for the variable field, the variable which needs to be filtered. It is the mandatory field.
  • filtername:This parameter stands for the name of the filter which the user wants to use. It is an optional parameter. If not specified, FILTER_DEFAULT is used, which means that not filtering would be done to the given variable.
  • options:This parameter is optional. It specifies the options/ flags to be used. It is basically an associative array of bitwise disjunctions of flags or options. If this parameter is used in the filter_var() function, a flag must be provided in the ‘flags’ field, and a callable type must be passed for the callback function. After accepting all the parameters, the filtered and sanitized variable is returned.

Return Value:The above function returns the filtered value or false if the data/ variable does not get filtered.

How does the filter_var function work in Php?

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.

Examples of PHP filter_var

Let us understand the working of the filter_var() function in Php along with the examples:

Example #1

Validating an Integer value using filter_var() function:

Code:

     
登入後複製

Output:

PHP filter_var

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

Example #2

Validating the IP address of the computer device using the filter_var() function

Code:

     
登入後複製

Output:

PHP filter_var

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

Example #3

Sanitizing and Validating the URL address using the filter_var() function

Code:

     
登入後複製

Output:

PHP filter_var

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

Example #4

Validating the email address of the user using the filter_var() function

Code:

     
登入後複製

Output:

PHP filter_var

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

Example #5

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:

PHP filter_var

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

Conclusion

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中文網其他相關文章!

相關標籤:
php
來源:php
本網站聲明
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn
最新下載
更多>
網站特效
網站源碼
網站素材
前端模板
關於我們 免責聲明 Sitemap
PHP中文網:公益線上PHP培訓,幫助PHP學習者快速成長!