You want to apply a piece of code to a range of integers.
Use a for loop. If you want to preserve the numbers for use beyond iteration, use the range() method.
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="EN" lang="EN">
<head>
<title>Series of Integers</title>
<meta Name="Author" Content="Hann So">
</head>
<body>
<p>
<?php
$start = 1;
$end = 10;
$increment = 2;
for ($i=$start; $i <= $end; $i++)
{
echo "The number is: $i<br />";
}
echo "<p>You can increment using values other than 1.</p>";
for ($i=$start; $i <= $end; $i +=$increment)
{
echo "The number is: $i<br />";
}
echo "<p>You use range().</p>";
$range = range($start, $end);
foreach ($range as $index =>$val) {
echo "$index: $val<br />";
}
?>
</p>
</body>
</html>
|