Check negative value in php

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

3,9852 gold badges17 silver badges15 bronze badges

asked May 26, 2011 at 7:44

BigJobbiesBigJobbies

4,24311 gold badges40 silver badges51 bronze badges

4

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.

answered May 26, 2011 at 7:45

4

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.

:-)

answered Aug 11, 2012 at 0:59

MahnMahn

15.8k16 gold badges60 silver badges77 bronze badges

6

if(x < 0)
if(abs(x) != x)
if(substr(strval(x), 0, 1) == "-")

answered May 26, 2011 at 7:52

Check negative value in php

MartyMarty

38.6k19 gold badges91 silver badges162 bronze badges

You could check if $profitloss < 0

if ($profitloss < 0):
    echo "Less than 0\n";
endif;

Shakti Singh

81.9k20 gold badges132 silver badges150 bronze badges

answered May 26, 2011 at 7:46

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

answered May 26, 2011 at 7:46

Tudor ConstantinTudor Constantin

25.6k7 gold badges48 silver badges71 bronze badges

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
'; } else { return 'It is not negative!
'; } } }

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!

answered Sep 30, 2017 at 23:38

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

answered Oct 22, 2015 at 11:33

Check negative value in php

1

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

echo ($profitloss < 0) ? 'false' : 'true';

Check negative value in php

answered Jan 23, 2015 at 10:47

1

Can be easily achieved with a ternary operator.

$is_negative = $profitloss < 0 ? true : false;

answered Nov 13, 2020 at 6:45

Usman AhmedUsman Ahmed

2,2711 gold badge17 silver badges17 bronze badges

1

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) === "-";
    }
}

answered Jul 2, 2021 at 6:57

Check negative value in php

How do you check if a number is negative in PHP?

check number is positive or negative in php.
if($number > 0){.
echo "the number is positive";.
elseif($number < 0){.
echo "the number is negative";.
echo "the number is zero";.

How do you check if a number is positive negative or zero in PHP?

php function CheckNumber($x) { if ($x >= 0) { if ($x > 0) $message = "Positive number"; else $message = "Zero"; } else $message = "Negative number"; echo $message. "\n"; } CheckNumber(5.5); CheckNumber(-10.8); ?>

How do you check the number is negative or not?

If you're not sure, here is the breakdown:.
If a number is greater than zero, it is a positive number..
If a number is less than zero, it is a negative number..
If a number equals to zero, it is zero..

How do I make a negative number positive in PHP?

The abs() function is an inbuilt function in PHP which is used to return the absolute (positive) value of a number. The abs() function in PHP is identical to what we call modulus in mathematics. The modulus or absolute value of a negative number is positive.