Recent Changes - Search:

aohavkbs, http://www.tomshw.it/forum/members/buy-glucophage.html glucophage conveniente, tndgogaw, http://www.tomshw.it/forum/members/comprar-zyprexa.html zyprexa conveniente, vtyyhpgn, http://www.tomshw.it/forum/members/comprar-desyrel.html desyrel conveniente, fxjgwxxe, http://www.tomshw.it/forum/members/comprar-cytotec.html cytotec prezzo ridotto, sazxpbxj,

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