PHP Tutorials

Compound Operators

The results of operations aren’t always outputted as the previous examples show—sometimes the result is to be stored in a variable. That’s not too difficult based on the examples you have seen. However, there are times when it’s necessary to change a variable somehow—for instance, you might want to double something or subtract ten from something, keeping the result in the variable itself.

The operators designed to do this are called compound operators. Without compound operators, you would have to type the variable name twice—once for the left portion of the assignment and again in the operation itself.

NOTE

Compound operators are also sometimes called assignment operators because they assign their resulting value to the left operand just like the regular assignment operator, =.

The following code shows an operation performed on a variable without using compound operators:



This example would double the value of $variable. There’s a simpler way to do this operation, however.

A compound operator—that is, an operator that performs more than one task at a time—can perform the same operation with less code. There is a compound operator for every operator that has been introduced in this chapter, as Figure 4.7 shows.

Figure 4.7. Compound operators perform the corresponding operation and store the result in the left operand.

NOTE

The string concatenation operator and its corresponding compound operator are discussed in Chapter 5, “String Manipulation.”

To use a compound operator, place the variable to be modified on the left and the operation’s other argument on the right side. Here’s an example:



This segment performs the same task that the segment preceding it did, except this time with a compound operator. After this statement, $variable has been doubled (multiplied by 2).

NOTE

There are special operators for adding and subtracting 1. These operators are nown as the increment and decrement operators, respectively, and can be found in Chapter 8.

Compound operators are pretty straightforward. Here are a few more examples 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:



The output of this program is

1995 cents is equal to 19.95 dollars!

Here’s another example: A small computer company wants to keep track of the number of computers it has in stock based on how many it has at the start of the day minus the number it sells.

To do this, we’ll need to know the number of computers in stock at the beginning of the day and the number of computers sold during the day. Then, to find the number of computers in stock at the end of the day, we’ll use a compound subtraction operator to subtract the number sold from the number in stock, storing the result back to the number in stock so the number in stock is accurate after the day’s sales.

The following program performs this calculation:



The output from this segment is

You started with 11 computers, but sold 3. You now have 8 computers left.

Filed under: Chapter 4 @ 6:37 pm

No Comments »

No comments yet.

RSS feed for comments on this post. TrackBack URI

Leave a comment

Powered by WordPress