Home of web learners

www.webn3rd.com

PHP has function to filter data

When you take input from front end side to backend server,you need to check data

Some times you want numerical data,but user give textual data,so need to filter

Lets look an example:

<?php
$data=100;
$data2="k";


if (filter_var($data, FILTER_VALIDATE_INT)) {
echo("Integer is valid");
} else {
echo("Integer is not valid"); }


if (filter_var($data2, FILTER_VALIDATE_INT)) {
echo(" Integer is valid");
} else {
echo(" Integer is not valid"); }


?>

Here,you see that we are going to validate that data need to be integer.

To check,first variable data we assign integer and second one is text,in if function we use the builtin function to filter data filter_var,here first one is variable,then second argument is which kind of data need to be as we check.

Here,if the variable data is integer then if is true and it shows Integer is valid nad for next check for $data2,if is false,so it execute else body block

webn3rd.com

About webn3rd