Check if a variable is an integer in PHP -
this question has answer here:
- checking if variable integer in php 11 answers
i doing pagination script , want give users ability control how many results shown in 1 page. doing through use of variable, this: example.org/articles.php?count=10
. problem variable must integer or code spits out random errors, of contains information user should not seeing.
here code:
// checks if there variable (this part works fine) if (isset($_get["count"])) { if (!empty($_get["count"])) { $page_rows = $_get["count"]; } else { $page_rows = $page_rows_default; } } else { $page_rows = $page_rows_default; } // checks if variable interger // if not, offending value replaced 0 // (it doesn't work) if(is_int($page_rows) == false) { $page_rows = 0; }
from experimentation code can tolerate zeros , negative integers, fails hard when given ?count=asdf
. not want user able crash script injecting random text variables. how script automatically detect non-integer values can dealt instead of halting code?
you can use is_numeric()
. reference http://php.net/manual/en/function.is-numeric.php
Comments
Post a Comment