You do not need a function just for this, to check if it is positive or negative, just use <
or >
, for example:
$input = 1;
if ($input > -1) {
echo 'Positivo';
} else if ($input < 0) {
echo 'Negativo';
}
Or <=
and >=
$input = 1;
if ($input >= 0) {
echo 'Positivo';
} else if ($input <= -1) {
echo 'Negativo';
}
Now about the behavior of
00
working and a
0
is not very strange, I assume you've messed up, anyway you can try using
parseFloat
, because you may not even be receiving a valid number , for example:
$adminAmount = parseFloat($adminAmount);
Yet it is best to check the format you received first
var_dump($adminAmount);
Using filter_var
and filter_input
You can use filter_var
to limit a range
, for example only accept numbers above 0
:
$adminAmount = filter_var($adminAmount,
FILTER_VALIDATE_INT,
array('options' => array('min_range' => 0)));
You control the range in array('min_range' => 0)
If the data comes from GET or POST you can use filter_input
:
$adminAmount = filter_input(INPUT_GET, 'admin-amount',
FILTER_VALIDATE_INT,
array('options' => array('min_range' => 0)));
'admin-amount'
is the name of <input>
If it's POST:
$adminAmount = filter_input(INPUT_POST, 'admin-amount',
FILTER_VALIDATE_INT,
array('options' => array('min_range' => 0)));