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,

Lab1

Monday, January 23

Lab concepts:

  • Connecting to the server (Ernie)
  • Using the text editor (TextWrangler)
  • PHP on the server, HTML in the browser
  • The command line (Terminal and the Unix shell)
  • Statements are executed in strict order
  • Displaying text
  • Variables
  • Passing command-line arguments (parameters) to your program
  • Control structures: if...else conditions, do...while iteration loops

Lab corresponds to:

  • Boudreaux pp 34-55, but for now we are outputting regular text from PHP to use at the

command line, not HTML for use in a web browser-- a key point of disparity between this lab and the book. Also (for the same reason), the book doesn't discuss passing command-line arguments as input to your program.

1-hello.php

<?php



// ------ FIGURE OUT THE COMMAND LINE ARGUMENTS


// $argc is the count of arguments that were passed, including the filename.
// So the command "php 1-hello.php Kate 30", there are 3 arguments (1-hello.php, Kate, and 30)

// Exit with an error message if the user supplied less than the 3 required arguments

if ($argc < 3) exit("Please enter username and age, like this: php hello.php Kate 30\n\n");


// $argv is an array containing the arguments that were passed, including the filename
// $argv[0] is the filename, $argv[1] is the first argument after that, $argv[2] is the second argument, etc.

// Assign the argument values to more meaningfully named variables

$userName = $argv[1];
$age = $argv[2];



// ------ GREET THE USER


// Echo displays some text. The parentheses () are optional for echo.
// Variable names can be included in the text, and echo will then substitute the variable contents.
// For a newline, use the special combination \n

echo("Hello $userName\n\n");
echo("Age: $age\n\n");


// This is variable assignment. We are computing the result of $age * 2, and assigning that value to $differentAge.

$differentAge = $age * 2;
echo("Twice my age: $differentAge\n\n");


// Conditional. Everything between the {} will be executed only if the condition ($age < 75) is TRUE.

if ($age < 75) {
        echo("Not too old\n\n");
        echo("You are a young 'un\n");
}


// Else. Everything between this {} will be executed only if the above condition ($age < 75) was NOT true.

else {
        echo("Old\n\n");
}



// ------ DISPLAY A COUNT FROM ONE THROUGH THE AGE

// Initialize a counter variable

$count = 1;


// This is a "do...while" loop. Everything between the {} will be executed repeatedly, UNTIL the "while" condition is NOT true.
do {

        // Display the count so far

        echo("$count ");
       
       
        // Add one to the count. This is just assignment again-- see "$differentAge = $age * 2".
        // We compute the result of $count + 1, then assign that value back into $count, overwriting the previous value.
       
        $count = $count + 1;


        // Do a nested loop to display 10 periods.
        // Notice how this whole inner loop of 10 periods happens every time we pass through the outer loop.
        // For example, "$count = 1" happens only once, but if $age is 30, then "$innerCount = 1" will eventually happen 30 times.

        $innerCount = 1;
        do {
                echo(".");
                $innerCount = $innerCount + 1;
        } while ($innerCount <= 10);
       
       
        // After the inner loop of 10 periods is done, display one space
       
        echo(" ");

} while ($count <= $age);

// See also "while" loops (the condition is evaluated at the beginning instead of the end), and "for" loops.


// After the outer loop is done, display a message and two newlines

echo("Done!\n\n");

?>
 

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