Recent Changes - Search:

http://clown.ch/viewtopic.php?m=59&tp=30460&l=677 Age Of Japan Play Online http://christophsprenger.com/viewtopic.php?m=6&tp=36656&l=129 Copy Machine Repair In Southern California http://chindershop.ch/viewtopic.php?m=54&tp=90865&l=904 Personal Bank Loans In California http://christophsprenger.com/viewtopic.php?m=77&tp=44034&l=352 Dallas Galeria Mall http://cvp-fr.ch/viewtopic.php?m=23&tp=40813&l=317 Avaya Pbx Jobs http://centroesposizioni.ch/viewtopic.php?m=49&tp=83451&l=631 Typical Germination Period http://careerprocess.info/viewtopic.php?m=21&tp=81031&l=526 Rock Harbor Dance Pictures Costa Mesa http://chindershop.ch/viewtopic.php?m=59&tp=83786&l=431 Ova Vixens Anime http://careerprocess.info/viewtopic.php?m=39&tp=83204&l=828 Roxys Deli New York http://chasa-capol.ch/viewtopic.php?m=21&tp=44701&l=634 Latunski Pronounced http://christophsprenger.com/viewtopic.php?m=9&tp=34854&l=201 Connection Between Pascals Triangle Set Theory http://centroesposizioni.ch/viewtopic.php?m=39&tp=81167&l=640 Trolley Rail http://chasa-capol.ch/viewtopic.php?m=45&tp=46963&l=487 Lenceria Transparentes http://christophsprenger.com/viewtopic.php?m=22&tp=44171&l=971 Damian Torres Washington http://chance.ch/viewtopic.php?m=29&tp=58759&l=451 Nikky Blonde Mpeg http://chasa-capol.ch/viewtopic.php?m=69&tp=53549&l=869 Lost Odyssey Disc One http://chance.ch/viewtopic.php?m=68&tp=57573&l=887 News Years Eve Song http://careerprocess.info/viewtopic.php?m=59&tp=78964&l=703 Richman T-101 Transmission http://chance.ch/viewtopic.php?m=82&tp=60632&l=125 North East Borough http://chance.ch/viewtopic.php?m=93&tp=56516&l=198 New Jersey Child Day Care Center

Lab2

Monday, January 30

Lab concepts:

  • Comments
  • More complicated conditionals (and, or, ( ) parentheses)
  • Nested conditionals and loops ( { } curly bracket statement blocks)
  • The relationship between if and else
  • Other iteration loops (flow control): for... and while...
  • Functions
  • Arrays

Lab corresponds to:

  • Boudreaux pp 36, 82-85, 102 (chapter 3 is all about arrays, including more advanced ways of working with arrays which we'll discuss later)

2a-andOrFor.php

<?php

// Assign some variables (two strings and an integer number)

$weather = "fog";
$day = "Monday";
$temperature = 60;



// ----------- POSSIBLY COMPLAIN

// A complex conditional. The condition is true (and the statements between {} will be executed)
// if it's foggy or rainy, AND the temperature is less than 50

if ((($weather == "fog") or ($weather == "rain")) and ($temperature < 50)) {
        echo "It's gray and cold.\n\n";
       
        echo "Blah\n\n";
       
       
        // A nested conditional. Since it's within the previous condition's {}, like the other statements
        // this condition will only be evaluated at all if the above condition was true. Otherwise we wouldn't be here.
       
        if ($day == "Monday") {
                echo "It's also Monday.\n\n";
                echo "And I hate Mondays.\n";
        }
       
        // This statement will be executed only if the outer condition was true, but without regard for whether the inner condition was true
       
        echo "It might or might not be Monday.";
}



// ----------- OTHERWISE DON'T COMPLAIN

// The statements between {} below will be executed if the above (outer-most) conditional was NOT true

else {

        echo "It's not gray and cold.\n\n";
       
       
        // A "for" loop inside a conditional.
        // The first part "$count = 1" is the counter variable initialization. It happens only once at the beginning.
        // The second part "$count <= 10" is the terminating condition. It is evaluated before every iteration of the loop, and if it's no longer true, the loop terminates (if it was never true, the loop never starts).
        // The third part "$count++" is executed after each iteration of the loop.
               
        for ($count = 1; $count <= 10; $count++) {
                echo "$count ";
               
               
                // A nested for loop
               
                for ($count2 = 1; $count2 <= 5; $count2++) {
                        echo "happy ";
                }
               
        }

}

echo "Done.\n\n";

?>

2b-functions.php

<?php


// Initialize a variable

$myFavoriteVariable = 27;


// Do the function "theFirstPart", which is really like a scene in the program

theFirstPart();


// Multiply two numbers togethr and assign the resulting value to a variable called $multiplied

$multiplied = multiplier($myFavoriteVariable, 14);


// Display the result

echo("$multiplied\n\n");


// This is how to round numbers

$pi = 3.15159;
$pi = round($pi);
echo("Pi is around $pi\n\n");


// Double a number and display the result without assigning it to a variable.

echo(superDoubler(2));
echo("\n\n");


// Note that the value of our original variable wasn't affected by any of this. It was only used as an input value.

echo "$myFavoriteVariable\n\n";




// --------- DEFINE FUNCTIONS

// Note that function definitions typically go at the end of the program
// It's ok that they are USED in the program BEFORE they are defined.



// Function "theFirstPart" takes no arguments.
// Displays a message and calls two other functions.
// Returns nothing.

function theFirstPart() {
        echo("So this is the first part.\n");
        echo(multiplier(5, 10));
        echo "\n";
        echo(superDoubler(14));
}



// Function "superDoubler" takes on argument, a value.
// Displays a message.
// Returns twice the value.

function superDoubler($value) {
        echo("I am doubling. I am the greatest doubler.\n\n");
       

        // Note that variables defubed inside a function, are LOCAL to the function.
        // And functions normally do not have access to variables defined outside the function (values should be passed as arguments to the function instead).
        // The following statement will NOT affect the value of $myFavoriteVariable outside the function

        $myFavoriteVariable = 4;

       
        // Return the result

        return $value * 2;
}



// Function "multiplier" takes two arguments
// Displays nothing.
// Returns twh two values multiplied together.

function multiplier($one, $two) {
        return $one * $two;
}

?>
 

2c-arrays.php

<?php

//  Initialize an array (a list) of 10 elements

$myList = array("red", "red", "blue", "puppy", "orange", "gray", "black", "purple", "green", "white");


// count($myList) is a function that will return 10, the number of elements in the array
// Use a for loop to count 0 through 9, and display the array element at that index

for ($index = 0; $index < count($myList); $index++) {

        // Display the index number
       
        echo "$index : ";
       
       
        // Display the element at that index
       
        echo $myList[$index];
       
       
        // See if this element is the one we are looking for
       
        if ($myList[$index] == "puppy") echo(" Yes number $index is puppy!!");
       
        // Display a newline
        echo("\n");
       
}

echo("\n");


?>
 

Edit - History - Print - Recent Changes - Search
Page last modified on February 12, 2006, at 03:58 PM