Reputation: 41
If I was checking a variable to see if it was a whole number in bash the coding would be similar to
if ! [[ "$yournumber" =~ ^[0-9]+$ ]] ; then
Is there a way in php to check if the variable contains a whole number?
Thank you all I guess I need to clarify that I want to evaluate that the number does not have a decimal value example is nuber 1 vs 1.2 or any other value with a decimal after it. Thanks in advance.
Upvotes: 2
Views: 1633
Reputation: 12776
For natural numbers (non-negative whole numbers) use ctype_digit
as other have suggested.
For whole numbers including negatives you can use this pattern: /\-?\d+/
.
Upvotes: 0
Reputation: 3519
There is also is_numeric()
, really depends on what you want to do actually.
Upvotes: 0
Reputation: 67502
You can use ctype_digit
for this:
if (ctype_digit($yournum)) {
// ...
}
PHP Docs:
Checks if all of the characters in the provided string, text, are numerical.
This does not include .
, as shown in their examples (on the page I linked).
Upvotes: 4
Reputation: 548
Perhaps this is what you're looking for?
is_int($var)
http://php.net/manual/en/function.is-int.php
Upvotes: -1
Reputation: 29925
You can use the function is_int()
to check if a variable contains an integer (whole number)
Upvotes: 0
Reputation: 324620
Exactly the same way: if( preg_match("/^[0-9]+$/",$yournumber))
Alternatively, try this: if( strval(intval($yournumber)) === strval($yournumber))
Upvotes: 0