| Implementing Arrays |
| Article Index |
|---|
| Implementing Arrays |
| Page 2 |
| Page 3 |
| Page 4 |
| Page 5 |
| Page 6 |
| Page 7 |
| Page 8 |
| Page 9 |
| Page 10 |
Although not as common in the newspaper today, cryptograms can be found in almost any puzzle book from your local supermarket. For those who are not familiar with them, the premise is quite simple: for a given string, replace every character (for instance, the letter A) with a different character. The challenge is then to take the encoded message and determine what the original message was. Although there are various ways to go about creating a cryptogram-generation script, we'll do so using a number of array functions starting with range().
In PHP, the range() function is used to create arrays that contain a specified range of numbers or letters. The syntax for range is as follows:
range(mixed $low, mixed $high)
$low represents the starting point of the range, and $high represents the end. As I have already alluded, these parameters can be numbers (that is, 1 through 10) or letters (that is, d through w). When executed, range() will return an integer-indexed array containing every number or letter between $low and $high. This function will be used to create an array containing all the letters in the alphabet, as well as the letters used to encode the cryptogram.
The second function that requires an introduction is the one actually responsible for determining how the letters are encoded in the final cryptogramthe shuffle() function. This function is similar to the array_rand() function discussed earlier in the chapter, with one significant difference. Instead of creating a new array randomly based on another array, the shuffle() function actually randomizes the contents of an array. The very simple syntax of the shuffle() is as follows:
shuffle($input)
Another function we'll need for this example is one that swaps the key/value pairs (so the keys are values, and the values are keys). To accomplish this, you'll use the array_flip() function, which also has a simple syntax:
| Users' Comments (0) |
|
No comment posted








