Web Development Institute is the premier institute in Delhi-NCR offering wide range of web design and web development courses. All our courses are designed by experts and in consultation with students from the advanced regions of the web world.

Read more

Our Web Development Courses are known for its professional excellence. Our highly qualified & experienced faculty offers intensified practical training to the students in the field of web development. They also render free career counseling.

Read more

Want to talk to our career advisor!! Feel free to contact on any of the following numbers:

Ring us at:   +91 9911-78-23-50
                      +91 9811-81-81-22

How to Use PHP Filters?

Learn about PHP_filters

I am sure you have learnt about PHP Filters in your PHP course. PHP filters are very useful in dealing with data security and data management. Data security and data management is a major concern in all PHP applications. Data management refers to the handling of data and maintaining it for use and processing of the application in PHP. Data for an application in PHP programs can have two sources, one could be the internal organization which runs the application and the other could be the external data. The data from the internal organization is easy to handle as it is pre-organized in the required formats. The main challenge faced while working with the external data. The external data could include the data from the user input, cookies, web services, server variables and database query results.

Once we receive an external data, we need to apply different functions on it to check for its file format, trimming, modifying it and so on. All these operations are done by the filters in PHP. Thus, we can understand, that the use of filters in PHP is very essential in any of the PHP based applications, specially for the ones with many user inputs. So, PHP Filters make sure that the external data is of the same type as required by our application.

Filters in PHP can be divided into various categories:

how to use php filters

Validate Filters in PHP

Validate filters are used to validate the data according to the specified conditions in PHP. It can apply conditions on both, the value and the type of the data. Some of the validate filter functions are:

  • FILTER_VALIDATE_BOOLEAN - This filter validates the value as a boolean option. It returns true for 1 and false for 0.
  • FILTER_VALIDATE_EMAIL - This filter validates the value as an email address.
  • FILTER_VALIDATE_FLOAT - This filter validates the value as a floating point number.
  • FILTER_VALIDATE_INT - This filter validates the value as an integer number.
  • FILTER_VALIDATE_IP - This filter validates the value as an ip address, only for IPv4 or IPv6 address and not from private or reserved range.
  • FILTER_VALIDATE_REGEXP - This filter validates the value against a regular expression.
  • FILTER_VALIDATE_URL - This filter validates the value as a URL.

Sanitize Filters in PHP

Sanitize filters are used to format the data in PHP coming from insecure sources. They allow or disallow specific characters in the data, can trim the data, format the data and return a string. Some of the sanitize filter functions are:

  • FILTER_SANITIZE_EMAIL - This filter removes all the characters, excepts letters, digits and !#$%&'*+-/=?^_`{|}~@.[].
  • FILTER_SANITIZE_ENCODED - This filter encodes the unwanted characters of a URL.
  • FILTER_SANITIZE_MAGIC_QUOTES - This filter adds backslashes in front of predefined characters like single quotes, double quotes, etc.
  • FILTER_SANITIZE_NUMBER_FLOAT - This filter removes all the characters, excepts digits, +- and optionally .,eE.
  • FILTER_SANITIZE_NUMBER_INT - This filter removes all the characters, except digits and +-.
  • FILTER_SANITIZE_SPECIAL_CHARS - This filter escapes "<>& and characters with ASCII values below 32.
  • FILTER_SANITIZE_STRING - This filter is used to encode unwanted characters. It removes the data harmful for the application.
  • FILTER_SANITIZE_STRIPPED - This filter is used to encode unwanted characters. It removes the data harmful for the application.
  • FILTER_SANITIZE_URL - This filter removes all the characters, except letters, digits and $-_.+!*'(),{}|\\^~[]`<>#%";/?:@&=.
  • FILTER_UNSAFE_RAW - This filter does nothing, or encodes and removes unwanted characters.
    • This filter removes data that is potentially harmful for your application. It is used to strip tags and remove or encode unwanted characters.

Callback Filters in PHP

Callback filters are used to call user defined functions to filter the data. They enable us to have full control over the data characters and type. This makes use of the FILTER_CALLBACK function.

Filter Flags in PHP

Filter flags are used to check the conditions and the results of the filter functions applied. All the filter functions have different set of filter flags. These flags help us to apply additional filtering options.

I am attaching a zip file of a complete form validation using PHP filters here. You can download and explore the real use of PHP filters in form validation in it.

Student’s Name: Tripti Goel
Course Name: Web Master
Guide By: Ravi Bhadauria