Docstoc

colors

Document Sample
colors Powered By Docstoc
					colorsbasic.php

<?php
$rainbow = array('red','orange','yellow','green','blue','purple');
// This sets up an array of 6 elements, note that the index/subscript 0 gets the first
element and 5 gets the last element
echo $rainbow[0];
echo "<br>";
echo $rainbow[5];
// The for loops through each element in the array
// Note that $ct = $ct + 1 could have been written as $ct++
for ($ct=0;$ct<6;$ct=$ct+1)
 {
   echo "<h2>The color is $rainbow[$ct]</h2>";
 }
?>

colorsforeach.php

<?php
$rainbow = array(0=>"red",1=>"orange",2=>"yellow",3=>"green",4=>"blue",5=>"purple");
// This sets up an array of 6 elements where we specify the key as we create the array
echo $rainbow[0];
echo "<br>";
echo $rainbow[5];
// The for loops through each element in the array

foreach($rainbow as $color)
 {
   echo "<h2> The color is $color</h2>";
 }
?>

colorsforeach1.php

<?php
$rainbow = array(0=>'red',3=>'orange',5=>'yellow',6=>'green',4=>'blue',2=>'purple',1=>'');
// This sets up an array of 6 elements where we specify the key as we create the array, note
the change in order
echo $rainbow[0];
echo "<br>";
echo $rainbow[5];
echo "<br>";
echo $rainbow[1];
echo "<br>";
echo $rainbow[2];

// The for loops through each element in the array and note that it ignores the specified
index and gives the order declared
$ct = 0;
foreach ($rainbow as $colors)
 {
   echo "<h2> The index is $ct and the color is $colors</h2>";
   $ct = $ct + 1;
 }
?>

colorsforeach2.php

<?php
$rainbow = array(0=>'red',3=>'orange',5=>'yellow',6=>'green',4=>'blue',2=>'purple',1=>'');
// This sets up an array of 6 elements where we specify the key as we create the array, note
the change in order
echo   $rainbow[0];
echo   "<br>";
echo   $rainbow[5];
echo   "<br>";
echo   $rainbow[1];
echo   "<br>";
echo   $rainbow[2];

// The for loops through each element in the array and note that it ignores the specified
index and gives the order declared

foreach ($rainbow as $indx=>$colors)
 {
   echo "<h2> The index is $indx and the color is $colors</h2>";

 }
?>

colorsspecifyindex.php

<?php
$rainbow = array(0=>'red',1=>'orange',2=>'yellow',3=>'green',4=>'blue',5=>'purple');
// This sets up an array of 6 elements where we specify the key as we create the array
echo $rainbow[0];
echo "<br>";
echo $rainbow[5];
// The for loops through each element in the array
// Note that $ct = $ct + 1 could have been written as $ct++
for ($ct=0;$ct<6;$ct=$ct+1)
 {
   echo "<h2> The index is $ct and the color is $rainbow[$ct]</h2>";
 }
?>

colorsspecifyindex1.php

<?php
$rainbow = array(0=>'red',3=>'orange',5=>'yellow',6=>'green',4=>'blue',2=>'purple',1=>'');
// This sets up an array of 6 elements where we specify the key as we create the array, note
the change in order
echo $rainbow[0];
echo "<br>";
echo $rainbow[5];
echo "<br>";
echo $rainbow[1];
echo "<br>";
echo $rainbow[2];

// The for loops through each element in the array
// Note that $ct = $ct + 1 could have been written as $ct++
for ($ct=0;$ct<7;$ct=$ct+1)
 {
   echo "<h2> The index is $ct and the color is $rainbow[$ct]</h2>";
 }
?>

colorsspecifyindex1a.php

<?php
$rainbow = array(0=>"red",3=>"orange",5=>"yellow",6=>"green",4=>"blue",2=>"purple",1=>"");
// This sets up an array of 6 elements where we specify the key as we create the array, note
the change in order
echo $rainbow[0];
echo "<br>";
echo   $rainbow[5];
echo   "<br>";
echo   $rainbow[1];
echo   "<br>";
echo   $rainbow[2];

// The for loops through each element in the array
// Note that $ct = $ct + 1 could have been written as $ct++
for ($ct=0;$ct<7;$ct=$ct+1)
 {
   echo "<h2> The index is $ct and the color is $rainbow[$ct]</h2>";
 }
?>

colorswordsub.php

<?php
$rainbow =
array("first"=>"red","second"=>"orange","third"=>"yellow","fourth"=>"green","fifth"=>"blue",
"sixth"=>"purple");
// This sets up an array of 6 elements where we specify the key as we create the array, note
the change in order

// The for loops through each element in the array and note that it ignores the specified
index and gives the order declared
foreach ($rainbow as $indx=>$colors)
 {
   echo "<h2> The index is $indx and the color is $colors</h2>";
 }
?>

				
DOCUMENT INFO
Categories:
Tags:
Stats:
views:7
posted:7/31/2012
language:
pages:3