Knowledge baseExam Center
ENEnglish
Choose language13

CKTIS / PHP

filter_var()

Apply a filter to a supplied value.

Version: PHP 8.4; repository runtime, production runtime not attested

Syntax

filter_var(mixed $value, int $filter = FILTER_DEFAULT, array|int $options = 0): mixed

Apply a filter to a supplied value. The default FILTER_UNSAFE_RAW does no validation. Integer validation may return the integer zero, so distinguish it from false with strict comparison. Numeric range options can reject an otherwise valid integer.

Signature

filter_var(mixed $value, int $filter = FILTER_DEFAULT, array|int $options = 0): mixed

Example output

[0,false,"not a number"]

Examples

<?php
$options=["options"=>["min_range"=>0,"max_range"=>4]];
echo json_encode([filter_var("0",FILTER_VALIDATE_INT,$options),filter_var("5",FILTER_VALIDATE_INT,$options),filter_var("not a number")]);

Result

[0,false,"not a number"]

Sources