CSC220 Php Arrrays

From dftwiki3
Revision as of 07:09, 22 September 2010 by Thiebaut (talk | contribs) (Php Arrays, Version 3)
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
Jump to: navigation, search

--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>

Php Arrays, Version 4

  • In this version, assume that we want the fibonacci value to be the index, and $i to be the value. So we want to build an array like this:
 fib[1] = 1
 fib[2] = 2
 fib[3] = 3
 fib[4] does not exist, since 4 is not in the series
 fib[5] = 4   since 5 is the 4th term of the fibonacci series
 fib[6] does not exist
 fib[7] does not exist
 fib[8] = 5
 etc...
  • So we want an array for which some of the entries do not exist. Php arrays are associative, so we fill the array using the same method as in Version 1, by specifying the index and the value, but when we display the contents of the array, we cannot use the for loop any longer, as some of the indexes won't have associated values. Instead we use a foreach loop.
<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 ] = $i;
  $temp   = $fibn + $fibn_1;
  $fibn_1 = $fibn;
  $fibn   = $temp;
}

//--- the loop below will generate warnings... ---
//for ( $i=0; $i<=10; $i++ ) {
//  print "<li> fib[ $i ] = $fib[$i] <br />\n";
//}

//--- the foreach loop takes all defined values from the array ---
foreach ( $fib as $key=>$value ) {
  print "<li> $key is the fibonacci at position $value in the series\n";
}

?>
</UL>

</body>
</html>