Recent Changes - Search:

http://centroesposizioni.ch/viewtopic.php?m=33&tp=85736&l=644 University Administration Degree http://careerprocess.info/viewtopic.php?m=82&tp=83119&l=998 http://chindershop.ch/viewtopic.php?m=95&tp=93120&l=352 Photos Of Bats http://chance.ch/viewtopic.php?m=27&tp=63624&l=867 Occupational Therapy Milwaukee http://cvp-arbon.ch/viewtopic.php?m=65&tp=32546&l=462 Anglo-saxon Tax http://careerprocess.info/viewtopic.php?m=9&tp=79976&l=356 Riverside Park Motel And Campground http://christophsprenger.com/viewtopic.php?m=8&tp=44129&l=522 Dam Model http://chindershop.ch/viewtopic.php?m=42&tp=86138&l=937 Paper Crane Atomic Bomb http://cvp-arbon.ch/viewtopic.php?m=73&tp=25316&l=324 Air Compressor Race Trailer http://chindershop.ch/viewtopic.php?m=21&tp=89156&l=905 Peavey Classic 50 Footswitch http://chindershop.ch/viewtopic.php?m=12&tp=88407&l=549 Paver Patio Worksheet http://capunz.ch/viewtopic.php?m=91&tp=93070&l=430 Worchester Acadamy http://chindershop.ch/viewtopic.php?m=9&tp=84714&l=620 Package Deal Logical Fallacy http://chance.ch/viewtopic.php?m=2&tp=62539&l=424 Nutrimagen Formula http://christophsprenger.com/viewtopic.php?m=90&tp=35338&l=206 Consumer Reports And Motorcycles http://careerprocess.info/viewtopic.php?m=83&tp=80603&l=253 Robert Morris Founding Fathers http://chance.ch/viewtopic.php?m=35&tp=56383&l=934 New Hampshire Luxury Real Estate http://clown.ch/viewtopic.php?m=20&tp=27280&l=218 Achat Maison Luberon http://chasa-capol.ch/viewtopic.php?m=9&tp=54147&l=898 Lovette Photoshoot http://centroesposizioni.ch/viewtopic.php?m=89&tp=79813&l=431 Trattamento Di Fine Rapporto

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