Check if input has negative value

Asked

Viewed 1,166 times

0

I have this example that checks if the input is only receiving numbers, in case it checks if it has only positive numbers, if I add for example a 0 it no longer works, I have to add two zeros 00, see:

elseif (!is_numeric($adminAmount)) {
    echo json_encode(array(
        'error'     => true,
        'message'   => 'A quantidade deve ser apenas números.'
    ));
}

If it is numbers continue if it does not it returns the error message.

Is there a native PHP function that checks whether the number is positive or negative? for example +1 or -1 ?

  • I didn’t understand anything you said here: por exemplo um 0 ele já não funciona, tenho que adicionar dois zeros 00

  • @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.

  • I don’t even know what the "working" behavior you’re referring to, it’s hard to understand that there :/

1 answer

1


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 even finish seeing everything, I knocked my eye on this example and got it, well, I have a criticism against PHP, if there is a function to check if it is string or numbers, I should have a native function to check if it is negative to facilitate our life aoksokoaks. obg waiting for the time to accept.

  • @Guilherme A function to check whether it is positive or negative seems to me quite redundancy, even more than the general problem seems that was some confusion on your part, however I will add another suggestion.

  • Whoops, every suggestion to me is welcome. 20 years of course po! kkkk obg ]

  • I think you confused parseFloat of javascript it does not exist in PHP. At least not that I know and went to search and really does not exist.

  • @Guilherme see the edition of the answer, about filter_var and filter_input

  • 1

    Oops, I solved the problem in the first example, but I’ll see the filter_input I think it gets more dynamic, and less gabiarra.

Show 1 more comment

Browser other questions tagged

You are not signed in. Login or sign up in order to post.