Search code examples
php4

what is the different between this in php?


what is the difference between

$totalprice += $product['price'] * $product['count'];

and

$totalprice = $product['price'] * $product['count'];

both give the same result. so what's the use of (+=) ?


Solution

  • += is a shorthand for adding the result to the target. The first one is equivalent to:

    $totalprice = $totalprice + ($product['price'] * $product['count']);

    There are also other compound operators -=, *=, /=, etc.