filter - PHP filter_input validate INT -



filter - PHP filter_input validate INT -

i have chunk of code:

$range = array ( 'options' => array ( 'min_range' => 0, 'max_range' => 10 ) ); if (!$number = filter_input(input_post, 'number', filter_validate_int, $range)) { exit ('error'); }

the "number" input sent options 0 10.

the problem if number = 0 returns "error".

what's wrong that?

thanks

this because !0 true. why? adding ! doing boolean check, variable gets converted. , according manual:

when converting boolean, next values considered false:

the boolean false itself

the integer 0 (zero)

the float 0.0 (zero)

the empty string, , string "0"

...

so integer 0 gets converted false while other integers converted true.

this why need type safe check false or null these values filter_input() returns if fails reason.

$number = filter_input(input_post, 'number', filter_validate_int, $range); if ($number === false) { //filter failed } if ($number === null) { //variable not set }

php filter

Comments

Popular posts from this blog

php - Android app custom user registration and login with cookie using facebook sdk -

django - Access session in user model .save() -

php - .htaccess Multiple Rewrite Rules / Prioritizing -