69

I was wondering if there is any way to detect if a number is negative in PHP?

I have the following code:

$profitloss = $result->date_sold_price - $result->date_bought_price;

I need to find out if $profitloss is negative and if it is, I need to echo out that it is.

jozala
  • 4,625
  • 2
  • 18
  • 15
BigJobbies
  • 4,293
  • 12
  • 42
  • 51

11 Answers11

197
if ($profitloss < 0)
{
   echo "The profitloss is negative";
}

Edit: I feel like this was too simple an answer for the rep so here's something that you may also find helpful.

In PHP we can find the absolute value of an integer by using the abs() function. For example if I were trying to work out the difference between two figures I could do this:

$turnover = 10000;
$overheads = 12500;

$difference = abs($turnover-$overheads);

echo "The Difference is ".$difference;

This would produce The Difference is 2500.

Dormouse
  • 5,130
  • 1
  • 26
  • 42
  • Nice answer, i like your edit as well. But what's the difference to `$difference = $turnover - $overheads;`? – Hannes Schneidermayer Jan 30 '14 at 08:33
  • 1
    Oh wow, I didn't expect to be commenting on this after almost three years. abs() will return the absolute value of any integer, so abs(-100) returns 100. `$turnover-$overheads` would result in -2500 whereas `abs($turnover-$overheads)` will result in 2500. – Dormouse Jan 30 '14 at 16:29
186

I believe this is what you were looking for:

class Expression {
    protected $expression;
    protected $result;

    public function __construct($expression) {
        $this->expression = $expression;
    }

    public function evaluate() {
        $this->result = eval("return ".$this->expression.";");
        return $this;
    }

    public function getResult() {
        return $this->result;
    }
}

class NegativeFinder {
    protected $expressionObj;

    public function __construct(Expression $expressionObj) {
        $this->expressionObj = $expressionObj;
    }

    public function isItNegative() {
        $result = $this->expressionObj->evaluate()->getResult();

        if($this->hasMinusSign($result)) {
            return true;
        } else {
            return false;
        }
    }

    protected function hasMinusSign($value) {
        return (substr(strval($value), 0, 1) == "-");
    }
}

Usage:

$soldPrice = 1;
$boughtPrice = 2;
$negativeFinderObj = new NegativeFinder(new Expression("$soldPrice - $boughtPrice"));

echo ($negativeFinderObj->isItNegative()) ? "It is negative!" : "It is not negative :(";

Do however note that eval is a dangerous function, therefore use it only if you really, really need to find out if a number is negative.

:-)

Mahn
  • 16,261
  • 16
  • 62
  • 78
23
if(x < 0)
if(abs(x) != x)
if(substr(strval(x), 0, 1) == "-")
Marty
  • 39,033
  • 19
  • 93
  • 162
7

You could check if $profitloss < 0

if ($profitloss < 0):
    echo "Less than 0\n";
endif;
Shakti Singh
  • 84,385
  • 21
  • 134
  • 153
andrewmitchell
  • 1,559
  • 13
  • 15
6
if ( $profitloss < 0 ) {
   echo "negative";
};
Tudor Constantin
  • 26,330
  • 7
  • 49
  • 72
5

Don't get me wrong, but you can do this way ;)

function nagitive_check($value){
if (isset($value)){
    if (substr(strval($value), 0, 1) == "-"){
    return 'It is negative<br>';
} else {
    return 'It is not negative!<br>';
}
    }
}

Output:

echo nagitive_check(-100);  // It is negative
echo nagitive_check(200);  // It is not negative!
echo nagitive_check(200-300);  // It is negative
echo nagitive_check(200-300+1000);  // It is not negative!
Muhammad Sanaullah
  • 435
  • 1
  • 6
  • 12
3

Just multiply the number by -1 and check if the result is positive.

2

You could use a ternary operator like this one, to make it a one liner.

echo ($profitloss < 0) ? 'false' : 'true';
Jaden Baptista
  • 656
  • 5
  • 16
AkaiNoKen
  • 21
  • 3
0

I assume that the main idea is to find if number is negative and display it in correct format.

For those who use PHP5.3 might be interested in using Number Formatter Class - http://php.net/manual/en/class.numberformatter.php. This function, as well as range of other useful things, can format your number.

$profitLoss = 25000 - 55000;

$a= new \NumberFormatter("en-UK", \NumberFormatter::CURRENCY); 
$a->formatCurrency($profitLoss, 'EUR');
// would display (€30,000.00)

Here also a reference to why brackets are used for negative numbers: http://www.open.edu/openlearn/money-management/introduction-bookkeeping-and-accounting/content-section-1.7

Vlad Vladimir Hercules
  • 1,781
  • 2
  • 20
  • 37
0

Can be easily achieved with a ternary operator.

$is_negative = $profitloss < 0 ? true : false;
Usman Ahmed
  • 2,392
  • 1
  • 20
  • 17
  • 1
    the expression is already a boolean value, so no need for ternary. its exactly the same as $is_negative = $profitloss < 0; – Nat Nov 18 '20 at 11:07
0

I wrote a Helper function for my Laravel project but can be used anywhere.

function isNegative($value){
    if(isset($value)) {
        if ((int)$value > 0) {
            return false;
        }
        return (int)$value < 0 && substr(strval($value), 0, 1) === "-";
    }
}
Nauman Bashir
  • 101
  • 1
  • 7