Difference between revisions of "CSC220 Php Arrrays"

From dftwiki3
Jump to: navigation, search
(Php Arrays, Version 1)
(Php Arrays, Version 2)
Line 51: Line 51:
 
$fibn_1 = 0;  // fib( n-1)
 
$fibn_1 = 0;  // fib( n-1)
 
for ( $i=0; $i<=10; $i++ ) {
 
for ( $i=0; $i<=10; $i++ ) {
  //print "<li> fib[ $i ] = $fibn <br />\n";
 
 
   $fib[ ] = $fibn;
 
   $fib[ ] = $fibn;
 
   $temp  = $fibn + $fibn_1;
 
   $temp  = $fibn + $fibn_1;
Line 67: Line 66:
 
</html>
 
</html>
 
</pre></code>
 
</pre></code>
 
  
 
=Php Arrays, Version 3=
 
=Php Arrays, Version 3=

Revision as of 06:57, 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++ ) {
  $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++ ) {
  $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>