Check using function is_numeric.
function round_num($var) {
if(is_numeric($var)) {
return round($var, 2);
}
}
Frage
Im trying to write a function that will check if a $var is a number (i was trying to trying to use is_int()) and if it is then run round().
The only problem is that for round() im using a number like 123.25, which as in understand isn't an int, because of the the decimal places, so i cant use is_int().
Any idea how i can get this to work ?
$var = 1123.25;
function round_num($var) {
if(is_int($var)) {
return round($var, 2);
}
}
echo round_num($var);
Lösung
Check using function is_numeric.
function round_num($var) {
if(is_numeric($var)) {
return round($var, 2);
}
}