代码改变世界

filter_var()函数解释

2012-01-25 20:27  youxin  阅读(445)  评论(0编辑  收藏  举报

The filter_var() function filters a variable with the specified filter.

Returns the filtered data on success or FALSE on failure.

Syntax

filter_var(variable, filter, options)
Optional. Specifies an associative array of flags/options or a single flag/option. Check each filter for possible options and flags
$int='123';
if(!filter_var($int,FILTER_VALIDATE_INT))
{
echo("integer is not valid");
}
else
{
echo ("integer is valid!");
}
输出valid

写options

$var=224;
$int_options=array("options"=>array("min_range"=>0,"max_range"=>256));
if(!filter_var($var,FILTER_VALIDATE_INT,$int_options))
{
echo "integer is not valid!";

}
else
{
echo "integer is valid!";
}

就像上面的代码一样,选项必须放入一个名为 "options" 的相关数组中。如果使用标志,则不需在数组内。

由于整数是 "300",它不在指定的氛围内,以上代码的输出将是 "Integer is not valid"。