Difference between revisions of "CSC220 Php Arrrays"
(Created page with '--~~~~ ---- __TOC__ =Php Arrays, Version 1= * In this version, the items are stored in the array at fixed indexes. THe programmer has to remember how to count (bad idea!) <cod…') |
(→Php Arrays, Version 3) |
||
Line 72: | Line 72: | ||
=Php Arrays, Version 3= | =Php Arrays, Version 3= | ||
+ | |||
+ | * In this version the array is predefined. The programmer may not have to know how to count, but will have to know how to add! | ||
<code><pre> | <code><pre> |
Revision as of 06:56, 22 September 2010
--D. Thiebaut 11:55, 22 September 2010 (UTC)
Php Arrays, Version 1
- In this version, the items are stored in the array at fixed indexes. THe programmer has to remember how to count (bad idea!)
<html>
<body>
<h1>Fibonacci Series</h1>
<h2>With Arrays</h2>
<h3>Version 1</h3>
<UL>
<?php
$fibn = 1; // fib(n)
$fibn_1 = 0; // fib( n-1)
for ( $i=0; $i<=10; $i++ ) {
//print "<li> fib[ $i ] = $fibn <br />\n";
$fib[ $i ] = $fibn;
$temp = $fibn + $fibn_1;
$fibn_1 = $fibn;
$fibn = $temp;
}
for ( $i=0; $i<=10; $i++ ) {
print "<li> fib[ $i ] = $fib[$i] <br />\n";
}
?>
</UL>
</body>
</html>
Php Arrays, Version 2
- in this version the terms are appended to the array. No need to declare the array, just start using a variable name with brackets after it, and it will be treated as an array.
<html>
<body>
<h1>Fibonacci Series</h1>
<h2>With Arrays</h2>
<h3>Version 2</h3>
<UL>
<?php
$fibn = 1; // fib(n)
$fibn_1 = 0; // fib( n-1)
for ( $i=0; $i<=10; $i++ ) {
//print "<li> fib[ $i ] = $fibn <br />\n";
$fib[ ] = $fibn;
$temp = $fibn + $fibn_1;
$fibn_1 = $fibn;
$fibn = $temp;
}
for ( $i=0; $i<=10; $i++ ) {
print "<li> fib[ $i ] = $fib[$i] <br />\n";
}
?>
</UL>
</body>
</html>
Php Arrays, Version 3
- In this version the array is predefined. The programmer may not have to know how to count, but will have to know how to add!
<html>
<body>
<h1>Fibonacci Series</h1>
<h2>With Arrays</h2>
<h3>Version 2</h3>
<UL>
<?php
// 0 1 2 3 4 5 6 7 8 9 10
$fib = array( 1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89 );
for ( $i=0; $i<=10; $i++ ) {
print "<li> fib[ $i ] = $fib[$i] <br />\n";
}
?>
</UL>
</body>
</html>