PHP Tutorials

Patterns and Arithmetic

Few uses of arithmetic will be just to modify a number and output the result. Instead, you will be using arithmetic to create patterns—however simple or complex they might be—to make more visitor-friendly, appealing programs.

The process of developing a pattern requires that you find an operation or set of operations that occur multiple times. For example, in making a table in which all of the rows are numbered in order, the pattern requires you to add one after each row. Repeating the process yields numbered rows—1, 2, 3, 4, and so on, as long as the pattern repetitions continue.

NOTE

Repetitions are achieved by a looping structure—that is, a statement telling PHP to execute one or more commands, what those commands are, and when to stop executing them. Looping statements are covered, along with some more examples of patterns, in Chapters 8 and 9.

Other patterns, however, may seem less obvious. For example, it is often easier to read a table if its alternating lines have alternating background colors. A result such as this is easy to imagine—but what kind of pattern could be used to create such an effect?

If you observe that numbers alternate between even and odd from one out to infinity, you have the first half of the pattern—create a variable that starts at one and increases by one each repetition. Then, all you have to do is test to find if the number is even or odd. More specifically, you have to test whether the number is evenly divisible by 2—if it is, it is even; if it isn’t, it is odd.

To test whether the number is evenly divisible, use modulus division to get the remainder and determine if the remainder is 1 or 0. Depending on the result, the appropriate background color can be chosen and the colors will alternate.

In parallel with that idea, if you wanted to use three different colors instead of two, you could find the remainder of the incrementing variable divided by 3. The result would be 0, 1, or 2, and depending on its value, the appropriate color could be used.

TIP

This example is discussed in more detail and is accompanied by sample code in Chapter 8.

Filed under: Chapter 4 @ 6:38 pm

What’s Next

In this chapter, you have learned about mathematical expressions and how they are evaluated in PHP. You have also learned about the math operators—including the four basic operations and an additional one for modulus (remainder) division. You’ve even skimmed the surface of applying arithmetic to patterns, which can obviously be quite handy.

From arithmetic, we’ll move into string manipulation. You can use string manipulation to read and interpret information from files, to replace certain words in a sentence or paragraph, or even to verify that an e-mail address a user gave you is valid. the next chapter will not only teach you several good uses of the string manipulation functions of PHP, but it will also help you to develop your own uses for them

Filed under: Chapter 4 @ 6:38 pm
« Previous Page

Powered by WordPress