check for integer or float values
Probably $number
is actually a string: "0.5"
.
See is_numeric
instead. The is_*
family checks against the actual type of the variable. If you only what to know if the variable is a number, regardless of whether it's actually an int
, a float
or a string
, use is_numeric
.
If you need it to have a non-zero decimal part, you can do:
//if we already know $number is numeric...
if ((int) $number == $number) {
//is an integer
}
checking if a number is float in PHP
If you change the first line to
$rewardAmt = $amt+0;
$rewardAmt should be cast to a number.
How to check if a number (float or integer) is within a range (0 - 100)
No need for regex:
if (is_numeric($val) && $val > 0 && $val <= 100)
{
echo '$val is number (int or float) between 0 and 100';
}
Demo
Update
It turns out you're getting the numeric values from a string. In that case, it would be best to extract all of them using a regex, something like:
if (preg_match_all('/\d+\.?\d*/', $string, $allNumbers))
{
$valid = [];
foreach ($allNumbers[0] as $num)
{
if ($num > 0 && $num <= 100)
$valid[] = $num;
}
}
You can leave out the is_numeric
check, because the matched strings are guaranteed to be numeric anyway...
php - check if a string contains a float
The easiest way would be to use built in function is_float(). To test if a variable is a number or a numeric string you must use is_numeric().
function to check if the value is int or float but not string in PHP
is_numeric($value) && !is_string($value)
this is what I was looking for
Checking if a string is a double or not
// Try to convert the string to a float
$floatVal = floatval($num);
// If the parsing succeeded and the value is not equivalent to an int
if($floatVal && intval($floatVal) != $floatVal)
{
// $num is a float
}
Need to check to enter only float numbers in php?
You have a couple of options ..
is_float() function
filter_var() function, with the FILTER_VALIDATE_FLOAT
filter as second argument.
floatval() or the (float)
cast before your variable name.
The first two will check wether or not it's a float, and the third one will convert whatever you give it to a float. I like to use the filter_var()
function as it makes the code clear, and it,s also very useful to verify all sort of things (emails adress, IP adress, URL, etc).
How can i detect if (float)0 == 0 or null in PHP
Using ===
checks also for the datatype:
$test = round(0, 2); // float(0.00)
if($test === null) // false
if($test === 0) // false
if($test === 0.0) // true
if($test === false) // false
Check if a number has a point
If you mean how to check if $result is float or integer so this is the way
$numberOne = 5;
$numberTwo = 8;
$result = $numberTwo / $numberOne;
if(is_float($result)){
echo "No comma!";
}else{
echo "Comma!";
}
Just replace comma and no comma...
Check if number is decimal
You can get most of what you want from is_float, but if you really need to know whether it has a decimal in it, your function above isn't terribly far (albeit the wrong language):
function is_decimal( $val )
{
return is_numeric( $val ) && floor( $val ) != $val;
}
Related Topics
Laravel - Database, Table and Column Naming Conventions
Stored Procedures, MySQL and PHP
How Come Checkbox State Is Not Always Passed Along to PHP Script
How to Calculate Time Difference in PHP
Does the Order of Class Definition Matter in PHP
How to Include a PHP.Ini File in Another PHP.Ini File
How to Execute PHP with Extension File.Php.Jpg
Bubble Sort Implementation in PHP
#1193 - Unknown System Variable 'Lc_Messages' When Trying to Login to PHPmyadmin
Setting Document Root for Laravel Project on Apache Virtual Host
PHP Run Once and Insert Twice in MySQL Database
Array_Map Function in PHP with Parameter
Checking If a Number Is Float in PHP
New Containing Div After Every 3 Records
Show Hide Payment Methods Based on Selected Shipping Method in Woocommerce
Convert Datetime to String PHP
MySQL Password Expired. Can't Connect
Remove All Non-Numeric Characters from a String; [^0-9] Doesn't Match as Expected