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
function and a 0
it’s not too weird, I assume you’ve made confusion, anyway you can try using the parseFloat
, because maybe you’re not even getting a valid number, for example:
$adminAmount = parseFloat($adminAmount);
Still the best is to check the format you received before all:
var_dump($adminAmount);
Using filter_var
and filter_input
You can use the 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 via 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 is POST:
$adminAmount = filter_input(INPUT_POST, 'admin-amount',
FILTER_VALIDATE_INT,
array('options' => array('min_range' => 0)));
I didn’t understand anything you said here:
por exemplo um 0 ele já não funciona, tenho que adicionar dois zeros 00
– Guilherme Nascimento
@Guilhermenascimento, Hare! type if I add 1 (one) 0 in the input it returns the message
json_encode
to work I have to put two(2) 00 in input.– user94336
I don’t even know what the "working" behavior you’re referring to, it’s hard to understand that there :/
– Guilherme Nascimento