You want to assign a series of consecutive integers to an array.
Use range($start, $stop):
$card = range(1, 52);
<!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>PHP</title>
<meta Name="Author" Content="Hann So">
</head>
<body>
<p>
<?php
$card = range(1, 52);
echo "<p><b>Display a deck of card</b></p>";
for ($i = 0; $i<=count($card); $i++)
echo "$card[$i] ";
// For increments other than 1
function pc_array_range($start, $stop, $step) {
$array = array();
for ($i = $start; $i<=$stop; $i += $step) {
$array[] = $i;
}
return $array;
}
// For odd numbers
$odd = pc_array_range(1, 52, 2);
echo "<p><b>Display odd numbers in a deck of card</b></p>";
// Use foreach to display
foreach ($odd as $value)
echo "$value ";
// For even numbers
$even = pc_array_range(2, 52, 2);
echo "<p><b>Display even numbers in a deck of card</b></p>";
// Use foreach to display
foreach ($even as $value)
echo "$value ";
// In PHP 5, just pass an increment to range() as a 3rd argument
$odd = range(1, 52, 2);
$even = range(2, 52, 2);
echo "<p><b>Display odd and even numbers in a deck of card using range() for PHP 5.</b></p>";
for ($i = 0; $i<count($even); $i++) {
echo "$odd[$i] $even[$i]<br />";
}
?>
</p>
</body>
</html>
|