BigJobbies BigJobbies - 3 years ago 139
PHP Question

Detecting negative numbers

I was wondering if there is anyway to detect if a number was 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 a negative ... if it is i need to echo out that it is.

Answer Source
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.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download