Skip to content
Advertisement

Is minus (negative) zero equivalent to 0 in PHP?

I have a very basic MySQL query that reads rows from a database table and adds or subtracts the row value to a PHP string defined as $total_balance.

For example;

$statement_details_query = mysqli_query($con,"SELECT transaction_nominal_code, SUM(transaction_debit) as TotalDebit, SUM(transaction_credit) as TotalCredit FROM accounts_transaction GROUP BY transaction_nominal_code") or die(mysql_error());

while($statement_details = mysqli_fetch_array( $statement_details_query )) {

$balance = $statement_details['TotalCredit'] - $statement_details['TotalDebit'];

$total_balance = $total_balance + $balance;

}

echo number_format($total_balance, 2, '.', ',');

My question is, what is the difference between -0 and 0?

Advertisement

Answer

In PHP, there is no real difference:

Float:

php > $negZ = -0.0;
php > $posZ = +0.0;
php > var_dump($negZ == $posZ, $negZ === $posZ);
bool(true)
bool(true)

Int:

php > $negZ = -0;
php > $posZ = +0;
php > var_dump($negZ == $posZ, $negZ === $posZ);
bool(true)
bool(true)
User contributions licensed under: CC BY-SA
5 People found this is helpful
Advertisement