filter_var
Filters a variable with a specified filter, used for validating and sanitizing data.
Parameters
main.php
Return Values
mixed
Returns the filtered data on success, or false on failure
On success:Validated/sanitized value according to the specified filter
On failure:false if validation fails or null if flag FILTER_NULL_ON_FAILURE is used
Notes
- Essential for input validation and security
- Supports both validation filters (FILTER_VALIDATE_*) and sanitization filters (FILTER_SANITIZE_*)
- Use FILTER_NULL_ON_FAILURE flag to return null instead of false
- More reliable than regex for common validation patterns
Changelog
- 8.0.0: FILTER_SANITIZE_MAGIC_QUOTES removed
- 7.3.0: FILTER_SANITIZE_ADD_SLASHES added
Related Functions
htmlspecialchars
SecurityConverts special characters to HTML entities to prevent XSS attacks and ensure safe HTML output.
securityxsssanitize
strlen
StringReturns the length of a string in bytes.
lengthvalidationsecurity
str_replace
StringReplaces all occurrences of search string with replacement string.
replacesanitizeclean
trim
StringReturns a string with whitespace (or other characters) stripped from the beginning and end of string.
cleanwhitespacesanitize
in_array
ArrayChecks if a value exists in an array.
searchexistsvalidate
password_hash
SecurityCreates a password hash using a strong one-way hashing algorithm for secure password storage.
securitypasswordhash