Check if integer _value_

/ Published in: PHP
Save to your folder(s)

PHP has is_numeric, and is_int, but this is a not a complete set. Is_numeric doesn't maintain that the value is strictly an integer, and is_int returns true if the TYPE of the variable is an integer. This returns whether a variable holds an integer value as either a true int, or as a string.

Copy this code and paste it in your HTML
  1. /**
  2.  * Check if a number is a counting number by checking if it
  3.  * is an integer primitive type, or if the string represents
  4.  * an integer as a string
  5.  */
  6. function is_int_val($data) {
  7. if (is_int($data) === true) return true;
  8. elseif (is_string($data) === true && is_numeric($data) === true) {
  9. return (strpos($data, '.') === false);
  10. }
  11. return false;
  12. }

Report this snippet


RSS Icon Subscribe to comments

You need to login to post a comment.