is_int(): accurately test for integer


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

A better and more accurate function to see if a value is an integer.


Copy this code and paste it in your HTML
  1. function is_int($int){
  2. if(is_numeric($int) === TRUE){
  3. return ((int)$int == $int) ? true: false;
  4. }
  5. return FALSE;
  6. }
  7. /*
  8.   // test code
  9. print("155 - ".int(155)."<br>");
  10.   print("15.5 - ".int(15.5)."<br>");
  11.   print("\"155\" - ".int("155")."<br>");
  12.   print("\"15.5\" - ".int("15.5")."<br>");
  13.   print("\"0155\" - ".int("0155")."<br>");
  14.   print("\"I'm 155\" - ".int("I'm 155")."<br>");
  15.   print("\"test\" -- ".int(true)."<br>");
  16.   print("\"\" - ".int(""));
  17. */

Report this snippet


Comments

RSS Icon Subscribe to comments

You need to login to post a comment.