Search code examples
phpsubtraction

function to get the subtraction result state that is it positive or negative?


I am performing subtraction on two variable.

$first_variable = 20;
$second_variable = 30;
$result = $first_variable - $second_variable;

So how do i get that the result $result is positive or negative? Have any PHP function to determine that the result of subtraction is positive or negative? I know the i can use if statement to get it done. but i am asking for any predefined function to do it.

The reason i asked it hear is just a curiosity.


Solution

  • You can use php gmp_sign function to achieve that check this

    Example:-

    <?php
    // positive
    echo gmp_sign("500") . "\n";
    
    // negative
    echo gmp_sign("-500") . "\n";
    
    // zero
    echo gmp_sign("0") . "\n";
    ?>
    

    output

    1
    -1
    0