if( (int) $a == $a)
{
// use $a
}
else
{
// use something else
}
"Please give us a simple answer, so that we don't have to think, because if we think, we might find answers that don't fit the way we want the world to be."
~ Terry Pratchett in Nation
I've trying to put one <input...> if the user enters a number and another <input...> if the user enters a string.
Basically I'm looking for it to search for an item reference number if they put in a reference number or search for keywords if they put in a text string.
You will probably either want to use a regular expression, or if it's a simple number then is_numeric. Bear in mind that since HTTP essentially uses a long string of text, any variable you receive in your PHP script is actually going to be a string type, so is_numeric should be used over is_int.
is_int() will not work with something like form inputs, as they are all strings. is_numeric() would "work", except "999.999" is numeric but not an integer. ctype_digit() would work as long as you realize that something like "9999999999999999999" would pass, but it could not be used as a "normal" signed 32-bit integer (but could be used with the BCMath functions as an integer). It all depends on what the ultimate requirements are. If you just want to ensure that the value consists only of the digits 0-9, ctype_digit() would be my preference. If you want to ensure that it is usable as a standard integer on your installation, I would go with the cast-to-integer option I suggested above.
"Please give us a simple answer, so that we don't have to think, because if we think, we might find answers that don't fit the way we want the world to be."
~ Terry Pratchett in Nation
Bookmarks