Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

is_int, is_numeric, is_float, and HTML form validation

A select field on my HTML form may yield 1 to 5 (integers). Using is_int rejects it every time, because the $_POST['rating'] is viewed as a string.

After consulting the PHP Manual, it seems is_numeric() && !is_float() is the proper way to validate for an integer in this case.

But I want to be certain, so please confirm or fire away at my logic.

like image 833
Drew Avatar asked Mar 30 '11 13:03

Drew


1 Answers

I would probably use something like this:

$value = filter_var(
  $_POST['rating'], 
  FILTER_VALIDATE_INT, 
  array('options' => array('min_range' => 1, 'max_range' => 5))); 

filter_var() will return either boolean false if the value is non-integer or out-of-range, or the valid value itself (as an integer.)

like image 116
Matt Gibson Avatar answered Sep 23 '22 20:09

Matt Gibson