PHP Numbers
Operating on a Series of Integers
Problem
Solution
Use a for loop:$start = 3;
$end = 7;
for ($i = $start; $i <= $end; $i++) {
printf("%d squared is %d\n", $i, $i * $i);
}
You can increment using values other than 1. For example:
$start = 3;
$end = 7;
for ($i = $start; $i <= $end; $i += 2) {
printf("The odd number %d squared is %d\n", $i, $i * $i);
}
If you want to preserve the numbers for use beyond iteration, use the range() method:
$numbers = range(3, 7);
foreach ($numbers as $n) {
printf("%d squared is %d\n", $n, $n * $n);
}
foreach ($numbers as $n) {
printf("%d cubed is %d\n", $n, $n * $n * $n);
}
Discussion
Loops like this are common. For instance, you could be plotting a function and need to calculate the results for multiple points on the graph. Or you could be a student countingdown the number of seconds until the end of school.
The for loop method uses a single integer and you have great control over the loop, because you can increment and decrement $i freely. Also, you can modify $i from inside the loop.
In the last example in the Solution, range() returns an array with values from $start to $end. The advantage of using range() is its brevity, but this technique has a disadvantage: a large array can take up unnecessary memory.
If you want range to skip steps, provide a third argument indicating how big each step should be. For example, range(1, 10, 2) returns an array containing 1, 3, 5, 7, 9. It’s also valid for $start to be larger than $end. In this case, the numbers returned by range() are in descending order.
range() can also be used to retrieve character sequences:
print_r(range('l', 'p'));
This prints:
Array
(
[0] => l
[1] => m
[2] => n
[3] => o
[4] => p
)
Note that the character sequences range() generates are just ASCII bytes, so it won’t work with multibyte Unicode characters.
Starting with PHP 5.5, you can use a generator to operate on a series. A generator is a function that, instead of calling return to return a value, calls yield (perhaps within a loop).
Then a call to that function can be used where you’d otherwise use an array, and you operate on the series of values passed to the yield keyword. For example, here’s how to use a generator to produce a list of squares:
function squares($start, $stop) {
if ($start < $stop) {
for ($i = $start; $i <= $stop; $i++) {
yield $i => $i * $i;
}
}
else {
for ($i = $stop; $i >= $start; $i--) {
yield $i => $i * $i;
}
}
}
foreach (squares(3, 15) as $n => $square) {
printf("%d squared is %d\n", $n, $square);
}
PHP keeps calling the squares() function as long as it calls yield. The key and value passed to yield can be used in the foreach just like a regular array element.
Generators are handy because you can have arbitrary behavior to create each value
(whatever you put inside your function) but the values are generated on demand. You don’t have to commit the memory (or processing) to create the whole array first, as with range(), before you start operating on it.
No comments:
Post a Comment