| Implementing Arrays |
| Article Index |
|---|
| Implementing Arrays |
| Page 2 |
| Page 3 |
| Page 4 |
| Page 5 |
| Page 6 |
| Page 7 |
| Page 8 |
| Page 9 |
| Page 10 |
Page 8 of 10
With all the initialization complete, you randomize your cryptogram lookup table using the shuffle() function discussed earlier and begin encoding your cryptogram (see ):
Listing 2.18. A Cryptogram Generator Using PHP Arrays (Part 2)
$encoded = "";
/* Cycle through each character of the message to encode */
for($i = 0; $i < strlen($message); $i++) {
$char = $message[$i];
/* Determine if the current character is encodable
by searching for it in the $cryptogram lookup table */
if(!in_array($char, $cryptogram)) {
/* if character is not encodable, copy straight to
the encoded string */
$encoded .= $char;
} else {
/* if the character is encoded, replace it with the
matching character from $cryptogram */
$encoded .= $cryptogram[$alphabet[$char]];
}
}
echo $encoded;
?>
NOTEAlthough not arrays, a string within PHP can behave like an array; it allows the developer to access a single character from the string by referencing it using the square-bracket syntax shown in . Note that although strings can behave as arrays, they cannot be used with array manipulation statements, such as foreach(), or array functions. |








