Compound operators in PHP

Sep 29, 2009 Author: Developer

Compound operators are pretty straightforward. Here is a example to help demonstrate their use. An organization is trying to break the world record for the longest distance of dollar bills strung together, but much of the money donated has been in change. In order to exchange the coins for bills, the group must calculate how many dollars the money is worth. To do this, we’ll need to know the amount in cents. Because 100 cents are in a dollar, dividing the amount in cents by 100 will yield the amount in dollars. Therefore, to convert the amount to dollars, we do a compound division operation using the amount and 100 as the operands. The following program converts an amount given in cents to a dollar amount:

$amount = 1995; // in cents
echo ”$amount cents is equal to”;
$amount /= 100; // 100 cents in a dollar
echo ”$amount dollars!”;

The output of this program is

1995 cents is equal to 19.95 dollars!

views 4209
  1. Add New Comment