Introduction to PHP. PHP vs CGI/Perl. Unix commands, installed Apache: post HTML documents. Static. Program Apache: use Perl, CGI (Common Gateway Interface), CGI.pm. Main issue: keeping state. Why: HTTP is connectionless. We developed a pattern: the program starts and 0. setup 1. read input (input and state) assumption is: you trust your user 2. is state empty? 2.1 if yes: initialize state 2.2 else: change state according to input 3. report state 4. save state 5. get ready for more input state: $message, $total, $good, $answer (a collection of variables) PHP: same thing setup: #!/usr/bin/perl use CGI; $q = new CGI; PHP: no need for a setup retrieve state (input): $message = $q->param('message'); $total = $q->param('total'); PHP: happens automatically initialize state: $message = "Welcome"; $total = 0; $good = 0; PHP: same way update state based on input: basic algorithm specific to that problem PHP: same save state and ger ready for more input: print qq{
Type your answer here: Press to continue.
}; PHP: basically same report state: print $message; PHP: scriptlet prints an expression same as programs in CGI/Perl are stored in ~/apache/cgi-bin PHP: programs in htdocs let's try an example: #!/usr/bin/perl use CGI; $q = new CGI; print $q->header, $q->start_html; $message = $q->param('message'); $secret = $q->param('secret'); $attempts = $q->param('attempts'); # state $userGuess = $q->param('userGuess'); # input if ($message) { # state is not empty if ($userGuess == $secret) { $message = "That's right, you won."; } elsif ($userGuess > $secret) { $message = "Nope, try lower."; $attempts += 1; } else { $message = "Nope, try higher."; $attempts += 1; } if ($attempts == 10) { $message = "You lost. The number was $secret."; } } else { # initialize $secret = int(rand(100)); $attempts = 0; $message = "Welcome"; } print qq{
$message

(The secret number is $secret, you have $attempts missed attempts thus far).

Guess the number:

Press to move on.

}; print $q->end_html; Using the equivalences mentioned above we convert this to: $secret) { $message = "Nope, try lower."; $attempts += 1; } else { $message = "Nope, try higher."; $attempts += 1; } if ($attempts == 10) { $message = "You lost. The number was $secret."; } } else { // initialize $secret = rand(0, 100); $attempts = 0; $message = "Welcome"; } ?>

(The secret number is , you have missed attempts thus far).

Guess the number:

Press to move on.

minute paper: a) how comfortable are you with perl given the above b) same question about cgi.pm the library c) how do you feel about php, in anticipation for thu; do you need any url for any tutorial you want to look over for thu or what do you think you need? (thu we discuss php by itself not by analogy like today and we should be able to discuss sessions as well). --end of lecture