
Wikis & learning
Count pages with a for loop
A for loop works well when both the first page and the last page are known.
A for loop works well when both the first page and the last page are known.
How it works
Choose an inclusive end condition when the last page should be printed. Increment once per iteration.
PHP example
<?php
for ($page = 1; $page <= 3; $page++) {
echo 'Page ' . $page . PHP_EOL;
}Expected result
Page 1, Page 2 and Page 3 on separate lines.
Try a variation
Change the upper limit to 1 and then to 0. Check how many lines each version prints.
Discussion
Sign in to participate.



