Difference between revisions of "CSC220 Php Arrrays"

From dftwiki3
Jump to: navigation, search
(Php Arrays, Version 3)
(Php Arrays, Version 3)
 
(3 intermediate revisions by the same user not shown)
Line 19: Line 19:
 
$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[ $i ] = $fibn;
 
   $fib[ $i ] = $fibn;
 
   $temp  = $fibn + $fibn_1;
 
   $temp  = $fibn + $fibn_1;
Line 35: Line 34:
 
</html>
 
</html>
 
</pre></code>
 
</pre></code>
 
  
 
=Php Arrays, Version 2=
 
=Php Arrays, Version 2=
Line 53: 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 69: Line 66:
 
</html>
 
</html>
 
</pre></code>
 
</pre></code>
 
  
 
=Php Arrays, Version 3=
 
=Php Arrays, Version 3=
Line 88: Line 84:
  
 
  for ( $i=0; $i<=10; $i++ ) {
 
  for ( $i=0; $i<=10; $i++ ) {
  print "<li> fib[ $i ] = $fib[$i] <br />\n";
+
    print "<li> fib[ $i ] = $fib[$i] <br />\n";
 
}
 
}
 
?>
 
?>
Line 98: Line 94:
 
</pre></code>
 
</pre></code>
  
 +
=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.
 +
 +
<code><pre>
 +
<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>
 +
</pre></code>
 
<br />
 
<br />
  

Latest revision as of 07:09, 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>

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>