csc 405: web application engineering ii 2.1 web programming with php introduction to web programming...

29
CSC 405: Web Application Engineering II 2.1 Web Programming with PHP Web Programming with PHP Introduction to Web programming Introduction to Web programming The programming language PHP — the first script The programming language PHP — the first script Embedding of code and comments Embedding of code and comments Variables and computation with numbers Variables and computation with numbers Something about character strings Something about character strings Conditional statements — if-statements Conditional statements — if-statements Loops — while-loops Loops — while-loops Use of form variables Use of form variables PHP scripts on the Web server PHP scripts on the Web server

Upload: cuthbert-crawford

Post on 18-Jan-2018

222 views

Category:

Documents


1 download

DESCRIPTION

CSC 405: Web Application Engineering II 2.3 Introduction to Web Programming HTTP is the protocol with which data is sent between a client and a Web server HTTP is the foundation for “the World Wide Web´´ Many different languages can be used for writing Web server programs (i.e., script): ASP, Java, Perl, TCL, C, C++, PHP, Scheme, Lisp, Standard ML, Erlang, Haskell, C#,...

TRANSCRIPT

Page 1: CSC 405: Web Application Engineering II 2.1 Web Programming with PHP Introduction to Web programming Introduction to Web programming The programming language

CSC 405: Web Application Engineering II 2.1

Web Programming with PHPWeb Programming with PHP Introduction to Web programmingIntroduction to Web programming

The programming language PHP — the first scriptThe programming language PHP — the first script

Embedding of code and commentsEmbedding of code and comments

Variables and computation with numbersVariables and computation with numbers

Something about character stringsSomething about character strings

Conditional statements — if-statementsConditional statements — if-statements

Loops — while-loopsLoops — while-loops

Use of form variablesUse of form variables

PHP scripts on the Web serverPHP scripts on the Web server

Page 2: CSC 405: Web Application Engineering II 2.1 Web Programming with PHP Introduction to Web programming Introduction to Web programming The programming language

CSC 405: Web Application Engineering II 2.2

Introduction to Web ProgrammingIntroduction to Web Programming

OVERVIEW:OVERVIEW:

Client (browser) requests a page on a Web serverClient (browser) requests a page on a Web server

Web server executes the programWeb server executes the program

The Web server program accesses (for instance) a database on the Web The Web server program accesses (for instance) a database on the Web server machineserver machine

The result of the execution (HTML code) is sent to the clientThe result of the execution (HTML code) is sent to the client

Page 3: CSC 405: Web Application Engineering II 2.1 Web Programming with PHP Introduction to Web programming Introduction to Web programming The programming language

CSC 405: Web Application Engineering II 2.3

Introduction to Web ProgrammingIntroduction to Web Programming

HTTP is the protocol with which data is sent between a client and a Web serverHTTP is the protocol with which data is sent between a client and a Web server

HTTP is the foundation for “the World Wide Web´´HTTP is the foundation for “the World Wide Web´´

Many different languages can be used for writing Web server programs (i.e., Many different languages can be used for writing Web server programs (i.e., script): ASP, Java, Perl, TCL, C, C++, script): ASP, Java, Perl, TCL, C, C++, PHP, Scheme, Lisp, Standard ML, PHP, Scheme, Lisp, Standard ML, Erlang, Haskell, C#, ...Erlang, Haskell, C#, ...

Page 4: CSC 405: Web Application Engineering II 2.1 Web Programming with PHP Introduction to Web programming Introduction to Web programming The programming language

CSC 405: Web Application Engineering II 2.4

Web Programming with PHPWeb Programming with PHP

PHP is a programming language for Web programmingPHP is a programming language for Web programmingExample — hello.php:Example — hello.php: <html><head><title>Hello World</title></head><html><head><title>Hello World</title></head> <body><body> <? echo "<p>Hi There, </p>";<? echo "<p>Hi There, </p>"; echo "<p>Greetings from a simple PHP script</p>"; ?>echo "<p>Greetings from a simple PHP script</p>"; ?> </body></body></html></html>

Page 5: CSC 405: Web Application Engineering II 2.1 Web Programming with PHP Introduction to Web programming Introduction to Web programming The programming language

CSC 405: Web Application Engineering II 2.5

Web Programming with PHPWeb Programming with PHP

PHP code is embedded in HTML code with the use of <? PHP code is embedded in HTML code with the use of <? and ?>and ?>

The PHP command The PHP command echoecho sends its argument (a sends its argument (a character string "...") to the browsercharacter string "...") to the browser

PHP commands are ended with the character ;PHP commands are ended with the character ; When a browser requests the page When a browser requests the page hello.phphello.php on the on the

Web server, the PHP code is executed, which results in Web server, the PHP code is executed, which results in HTML code, which again is sent to the browser:HTML code, which again is sent to the browser:

<html><head><title>Hello World</title></head><html><head><title>Hello World</title></head> <body><body> <p>Hi There, </p><p>Greetings from a simple <p>Hi There, </p><p>Greetings from a simple

PHP script</p>PHP script</p> </body></body></html></html>

Page 6: CSC 405: Web Application Engineering II 2.1 Web Programming with PHP Introduction to Web programming Introduction to Web programming The programming language

CSC 405: Web Application Engineering II 2.6

Code Embeddings and CommentsCode Embeddings and Comments The following example illustrates three ways of embedding The following example illustrates three ways of embedding

code in a PHP document — code in a PHP document — embed.phpembed.php:: <html><head><title>Embeddings</title></head><html><head><title>Embeddings</title></head> <body> <h2>Tree forms for embedding PHP in HTML</h2><body> <h2>Tree forms for embedding PHP in HTML</h2> <ol><ol> <? echo "<li>The simple form</li>"; ?><? echo "<li>The simple form</li>"; ?> <?php echo "<li>A slightly longer form</li>"; ?><?php echo "<li>A slightly longer form</li>"; ?> <script language="PHP"><script language="PHP"> // Comments in PHP code is not sent to the browser// Comments in PHP code is not sent to the browserecho "<li>The somewhat longer form</li>";echo "<li>The somewhat longer form</li>"; </script></script> </ol></ol> </body></body></html></html> Usually, we shall use the simple form <? ... ?>Usually, we shall use the simple form <? ... ?> Comments that extend over several lines can be written Comments that extend over several lines can be written

/* ... *//* ... */

Page 7: CSC 405: Web Application Engineering II 2.1 Web Programming with PHP Introduction to Web programming Introduction to Web programming The programming language

CSC 405: Web Application Engineering II 2.7

Variables in PHPVariables in PHP Variables are used for storing values during the execution of a Variables are used for storing values during the execution of a

PHP scriptPHP script Values can, for instance, be character strings and numbersValues can, for instance, be character strings and numbers Names of variables are chosen freely by the programmer, Names of variables are chosen freely by the programmer,

although variable names must start with the character $although variable names must start with the character $ Example — Example — homepage.phphomepage.php:: <html><head><title>Home page</title></head><html><head><title>Home page</title></head> <body><body> <h2> <? $name = "Martin Elsman";<h2> <? $name = "Martin Elsman"; echo "Home page for ";echo "Home page for "; echo $name;echo $name; ?>?> </h2><hr />This page is maintained by</h2><hr />This page is maintained by <? echo $name ?><? echo $name ?> </body></html></body></html> Notice that a variable can be referred to more than once after Notice that a variable can be referred to more than once after

it has been initialized with a value.it has been initialized with a value.

Page 8: CSC 405: Web Application Engineering II 2.1 Web Programming with PHP Introduction to Web programming Introduction to Web programming The programming language

CSC 405: Web Application Engineering II 2.8

Computation with NumbersComputation with Numbers There are two types of numbers in PHP:There are two types of numbers in PHP:

1. Integers: 3, 9, 0, -1, ...2. Doubles (double precision): 3.0, 9.2, 0.13, -23.2, ...

The usual number operations (e.g., +,-,*, and /) can be used in The usual number operations (e.g., +,-,*, and /) can be used in computations. computations.

Example — Example — dollars.phpdollars.php:: <html><head><title>Dollars</title></head><html><head><title>Dollars</title></head> <body><body> <h2>Dollars</h2><h2>Dollars</h2> <? $rate = 8.43;<? $rate = 8.43; $kroner = 1000.0;$kroner = 1000.0; $dollars = ($kroner - 20.0) / $rate;$dollars = ($kroner - 20.0) / $rate; echo "For DKr. $kroner you receive \$$dollars"; ?>echo "For DKr. $kroner you receive \$$dollars"; ?> </body></body> </html></html> Notice: It is possible to refer to a variable in a character string "...“Notice: It is possible to refer to a variable in a character string "...“ To insert a dollar-sign ($) in a character string, the character \ To insert a dollar-sign ($) in a character string, the character \

should be placed before the dollar-signshould be placed before the dollar-sign

Page 9: CSC 405: Web Application Engineering II 2.1 Web Programming with PHP Introduction to Web programming Introduction to Web programming The programming language

CSC 405: Web Application Engineering II 2.9

Arithmetic Operations and Evaluation Order Arithmetic Operations and Evaluation Order (precedence)(precedence)

Operators with a high precedence bind stronger than operators with Operators with a high precedence bind stronger than operators with a low precedence, and are therefore evaluated first.a low precedence, and are therefore evaluated first.

The operators *, /, and % have higher precedence than + and -, The operators *, /, and % have higher precedence than + and -, thus operations with these operators are evaluated first.thus operations with these operators are evaluated first.

When operators have the same precedence (same degree of When operators have the same precedence (same degree of binding), evaluation goes from left to right.binding), evaluation goes from left to right.

Page 10: CSC 405: Web Application Engineering II 2.1 Web Programming with PHP Introduction to Web programming Introduction to Web programming The programming language

CSC 405: Web Application Engineering II 2.10

Arithmetic Operations and Evaluation Order Arithmetic Operations and Evaluation Order (precedence)(precedence)

Example: What is the type and value of the following expressions?Example: What is the type and value of the following expressions?

It is possible to construct arbitrarily complicated expressions, for It is possible to construct arbitrarily complicated expressions, for instance:instance:22 - 34 + 43 % 34 * 23 + 122 / 43.22 * 23 + 43 evaluates to 302.92422 - 34 + 43 % 34 * 23 + 122 / 43.22 * 23 + 43 evaluates to 302.924

Without precedence rules, expressions can be ambiguous: Does 2+4*4 Without precedence rules, expressions can be ambiguous: Does 2+4*4 evaluate to 24 or 18?evaluate to 24 or 18?

Because * has a higher precedence than +, 4*4 is evaluated first Because * has a higher precedence than +, 4*4 is evaluated first whereafter 2 is added.whereafter 2 is added.

To evaluate the addition first, parentheses can be used: The expression To evaluate the addition first, parentheses can be used: The expression (2+4)*4 evaluates to 24.(2+4)*4 evaluates to 24.

Because and = have the same precedence, 2*5/2 is evaluated from left Because and = have the same precedence, 2*5/2 is evaluated from left to right, resulting in the value 5.to right, resulting in the value 5.

To evaluate the division first, parentheses can be used: The expression To evaluate the division first, parentheses can be used: The expression 2*(5/2) evaluates to the value 4.2*(5/2) evaluates to the value 4.

Page 11: CSC 405: Web Application Engineering II 2.1 Web Programming with PHP Introduction to Web programming Introduction to Web programming The programming language

CSC 405: Web Application Engineering II 2.11

More About Character StringsMore About Character Strings Character strings in PHP can be expressed either by "..." or by ’...’.Character strings in PHP can be expressed either by "..." or by ’...’. In character strings on the form ’...’, variables cannot be referred to.In character strings on the form ’...’, variables cannot be referred to. Example: If the variable $name contains the value Per, the two Example: If the variable $name contains the value Per, the two

statementsstatements

echo "Your name is $name";echo "Your name is $name"; echo ’Your name is $name’;echo ’Your name is $name’;

result inresult in

Your name is Per Your name is $name

Other special characters in character strings on the form "...":Other special characters in character strings on the form "...": \\ : backslash \n : newline \t : tabulator \" : double quote

Page 12: CSC 405: Web Application Engineering II 2.1 Web Programming with PHP Introduction to Web programming Introduction to Web programming The programming language

CSC 405: Web Application Engineering II 2.12

Appending of Character StringsAppending of Character Strings Character strings can be appended in PHP by using the character Character strings can be appended in PHP by using the character

‘.’‘.’

Example — Example — strings.phpstrings.php::

<<html><head><title>Character Strings</title></head>html><head><title>Character Strings</title></head> <body><body> <? $firstname = "Martin";<? $firstname = "Martin"; $lastname = "Elsman";$lastname = "Elsman"; $name = $firstname . " " . $lastname;$name = $firstname . " " . $lastname; echo "My name is $name";echo "My name is $name"; ?>?> </body></body></html></html>

Page 13: CSC 405: Web Application Engineering II 2.1 Web Programming with PHP Introduction to Web programming Introduction to Web programming The programming language

CSC 405: Web Application Engineering II 2.13

Conditional Statements in PHPConditional Statements in PHP If-statements are used for executing different code If-statements are used for executing different code

depending on some depending on some conditioncondition Example — Example — account.phpaccount.php:: <html><head><title>Account</title></head><html><head><title>Account</title></head> <body><body> <? $dollars = 8;<? $dollars = 8; if ( $dollars == 1 ) { echo "You have 1 Dollar on you if ( $dollars == 1 ) { echo "You have 1 Dollar on you

account"; // if-branchaccount"; // if-branch } else { echo "You have $dollars Dollars on your } else { echo "You have $dollars Dollars on your

account"; // else-branchaccount"; // else-branch }} ?>?> </body></html></body></html> A condition is either FALSE (the value 0) or TRUE A condition is either FALSE (the value 0) or TRUE

(different from 0)(different from 0) Other condition operatorsOther condition operators

< less than, > larger than, != different from, <= less than or equal, >= larger than or equal

Page 14: CSC 405: Web Application Engineering II 2.1 Web Programming with PHP Introduction to Web programming Introduction to Web programming The programming language

CSC 405: Web Application Engineering II 2.14

The If-statement in PHPThe If-statement in PHP The general format:The general format:

if ( if ( condition1 ) {condition1 ) { statement1statement1 } elseif ( } elseif ( condition2 ) {condition2 ) { statement2statement2 } else {} else { statement3statement3 }}

Meaning:Meaning: 1. compute the value of condition1. 2. if different from 0 (i.e., TRUE) then execute statement1,

otherwise 3. compute the value of condition2 4. if different from 0 (i.e., TRUE) then execute statement2,

otherwise 5. execute statement3

An arbitrary number of An arbitrary number of elseifelseif-branches can be used.-branches can be used. It is possible to omit the closing else-branch.It is possible to omit the closing else-branch.

Page 15: CSC 405: Web Application Engineering II 2.1 Web Programming with PHP Introduction to Web programming Introduction to Web programming The programming language

CSC 405: Web Application Engineering II 2.15

The If-statement in PHPThe If-statement in PHP Example: Body Mass Index — Example: Body Mass Index — bmi.phpbmi.php

<html><head><title>Body Mass Index</title></head><html><head><title>Body Mass Index</title></head> <body><body> <h2>Body Mass Index</h2><h2>Body Mass Index</h2> <?php $weight = 70.0; $height = 180.0;<?php $weight = 70.0; $height = 180.0; echo "Weight: $weight kg. <br />Height: $height cm.<br />";echo "Weight: $weight kg. <br />Height: $height cm.<br />"; $bmi = $weight / (($height/100.0) * ($height/100.0));$bmi = $weight / (($height/100.0) * ($height/100.0)); echo "Your BMI is $bmi<br />";echo "Your BMI is $bmi<br />"; if ( $bmi < 20.0 ) { echo "Your BMI is too low.";if ( $bmi < 20.0 ) { echo "Your BMI is too low."; } elseif ( $bmi < 25.0 ) {} elseif ( $bmi < 25.0 ) { echo "Your BMI is normal.";echo "Your BMI is normal."; } else {} else { echo "Your BMI is too high.";echo "Your BMI is too high."; }} ?>?> </body></html></body></html>

Page 16: CSC 405: Web Application Engineering II 2.1 Web Programming with PHP Introduction to Web programming Introduction to Web programming The programming language

CSC 405: Web Application Engineering II 2.16

Comparison Operators and their PrecedenceComparison Operators and their Precedence

The arithmetic operators *, /, %, +, - bind stronger than the The arithmetic operators *, /, %, +, - bind stronger than the comparison operators <, <=, >, >=.comparison operators <, <=, >, >=.

The comparison operators <, <=, >, >= bind stronger than == and !The comparison operators <, <=, >, >= bind stronger than == and !=.=.

Page 17: CSC 405: Web Application Engineering II 2.1 Web Programming with PHP Introduction to Web programming Introduction to Web programming The programming language

CSC 405: Web Application Engineering II 2.17

Comparison Operators and their PrecedenceComparison Operators and their PrecedenceExampleExample

Let x have the value 2 and y have the value 4.Let x have the value 2 and y have the value 4.

Remember: The number 1 denotes TRUE and 0 denotes FALSE.Remember: The number 1 denotes TRUE and 0 denotes FALSE.

Page 18: CSC 405: Web Application Engineering II 2.1 Web Programming with PHP Introduction to Web programming Introduction to Web programming The programming language

CSC 405: Web Application Engineering II 2.18

Logical Operators and their PrecedenceLogical Operators and their Precedence

The operator ! binds stronger than &&, which binds stronger than ||.The operator ! binds stronger than &&, which binds stronger than ||. ! also binds stronger than the comparison operators and the ! also binds stronger than the comparison operators and the

arithmetic operators.arithmetic operators. && and || bind weaker than the comparison operators and the && and || bind weaker than the comparison operators and the

arithmetic operators.arithmetic operators. Evaluation goes from left to right.Evaluation goes from left to right. If If exp1 is FALSE in exp1 && exp2 then exp2 is not evaluated.exp1 is FALSE in exp1 && exp2 then exp2 is not evaluated. If If exp1 is TRUE in exp1 || exp2 then exp2 is not evaluated.exp1 is TRUE in exp1 || exp2 then exp2 is not evaluated.

Page 19: CSC 405: Web Application Engineering II 2.1 Web Programming with PHP Introduction to Web programming Introduction to Web programming The programming language

CSC 405: Web Application Engineering II 2.19

Logical Operators and their PrecedenceLogical Operators and their Precedence

Example: What is the result of the following logical expressions?Example: What is the result of the following logical expressions?

Let x = 2 and y = 4.Let x = 2 and y = 4.

Remember: the value 1 denotes TRUE and 0 denotes FALSE.Remember: the value 1 denotes TRUE and 0 denotes FALSE.

Page 20: CSC 405: Web Application Engineering II 2.1 Web Programming with PHP Introduction to Web programming Introduction to Web programming The programming language

CSC 405: Web Application Engineering II 2.20

Loops in PHPLoops in PHP

How can we output the text "I love Web programming" 20 times?How can we output the text "I love Web programming" 20 times?Bad solution:Bad solution:

echo "I love Web programming<br />";echo "I love Web programming<br />"; ...18 times......18 times... echo "I love Web programming<br />";echo "I love Web programming<br />";

Better solution: use a while-loop for repetition — Better solution: use a while-loop for repetition — love.phplove.php::

$counter = 20;$counter = 20; while ( $counter >= 1 ) {while ( $counter >= 1 ) { E cho "I love Web programming<br />";E cho "I love Web programming<br />"; $counter = $counter - 1;$counter = $counter - 1; }}

Page 21: CSC 405: Web Application Engineering II 2.1 Web Programming with PHP Introduction to Web programming Introduction to Web programming The programming language

CSC 405: Web Application Engineering II 2.21

Loops in PHPLoops in PHP

The statement The statement echoecho is executed 20 times, with is executed 20 times, with $counter = 20, $counter = 20, 19, . . . , 119, . . . , 1

It is important that the content of the variable $It is important that the content of the variable $countercounter (i.e., a (i.e., a number) decreases for each execution of the loop-body.number) decreases for each execution of the loop-body.

What happens if the variable $counter is not decreased?What happens if the variable $counter is not decreased?

Syntax for while-loopsSyntax for while-loops

while ( while ( condition ) {condition ) { statement ;statement ; }} Meaning:Meaning:

1. Evaluate the condition2. If the result is different from 0 (i.e., TRUE), then evaluate

statement, and continue at (1)

Page 22: CSC 405: Web Application Engineering II 2.1 Web Programming with PHP Introduction to Web programming Introduction to Web programming The programming language

CSC 405: Web Application Engineering II 2.22

Loops in PHPLoops in PHP

Thus, the Thus, the body of the while-loop (statement) is evaluated as long as the body of the while-loop (statement) is evaluated as long as the condition is TRUEcondition is TRUE

Often used while-construction — Often used while-construction — love.phplove.php:: initialization ;initialization ; while ( while ( condition ) {condition ) { statement ;statement ; increment ;increment ; }} Examples of while-loops — Examples of while-loops — loops1.phploops1.phpStandard loop, where $i takes the values ___, ___, ___, ___, ___, ___, ___, Standard loop, where $i takes the values ___, ___, ___, ___, ___, ___, ___,

___, ___, ___, ___:___, ___, ___, ___: $i = 10;$i = 10; while ( $i >= 1 ) {while ( $i >= 1 ) { $i = $i - 1;$i = $i - 1; }} echo "Loop Example 1: $i <br />";echo "Loop Example 1: $i <br />";

Page 23: CSC 405: Web Application Engineering II 2.1 Web Programming with PHP Introduction to Web programming Introduction to Web programming The programming language

CSC 405: Web Application Engineering II 2.23

Loops in PHPLoops in PHP

Standard loop, where $i takes the values ___, ___, ___, ___, ___, Standard loop, where $i takes the values ___, ___, ___, ___, ___, ___:___:

$i = 1;$i = 1; while ( $i <= 10 ) {while ( $i <= 10 ) { $i = $i + 2;$i = $i + 2; }} echo "Loop Example 2: $i <br />";echo "Loop Example 2: $i <br />";

Standard loop, where $i takes the values ___, ___, ___, ___, ___, Standard loop, where $i takes the values ___, ___, ___, ___, ___, ___, ___, ___:___, ___, ___:

$i = 1;$i = 1; while ( $i <= 100 ) {while ( $i <= 100 ) { $i = $i * 2;$i = $i * 2; }} echo "Loop Example 3: $i <br />";echo "Loop Example 3: $i <br />"; What is the value of $i after each loop?What is the value of $i after each loop?

Page 24: CSC 405: Web Application Engineering II 2.1 Web Programming with PHP Introduction to Web programming Introduction to Web programming The programming language

CSC 405: Web Application Engineering II 2.24

Loops in PHPLoops in PHP Loop Exercises with while — Loop Exercises with while — loops2.phploops2.php

Write a while-loop that outputs 64, 32, 16, 8, 4, 2, 1:Write a while-loop that outputs 64, 32, 16, 8, 4, 2, 1:

$i = __ ;$i = __ ; while ( __ >= __ ) {while ( __ >= __ ) { echo "$i, ";echo "$i, "; $i = __ / __ ;$i = __ / __ ; }}

Write a while-loop that outputs 2, 4, 6, 8, . . . , 100:Write a while-loop that outputs 2, 4, 6, 8, . . . , 100:

$i = __ ;$i = __ ; while ( __ <= __ ) {while ( __ <= __ ) { echo "$i, ";echo "$i, "; $i = $i + __ ;$i = $i + __ ; }}

Page 25: CSC 405: Web Application Engineering II 2.1 Web Programming with PHP Introduction to Web programming Introduction to Web programming The programming language

CSC 405: Web Application Engineering II 2.25

Loops in PHPLoops in PHP

Write a while-loop that outputs 100, 110, 120, . . . , 200:Write a while-loop that outputs 100, 110, 120, . . . , 200:

$i = __ ;$i = __ ; while ( $i <= __ ) {while ( $i <= __ ) { echo "$i, ";echo "$i, "; $i = $i + __ ;$i = $i + __ ; } }

Page 26: CSC 405: Web Application Engineering II 2.1 Web Programming with PHP Introduction to Web programming Introduction to Web programming The programming language

CSC 405: Web Application Engineering II 2.26

Use of Form VariablesUse of Form Variables

It is possible for PHP code to use data submitted by It is possible for PHP code to use data submitted by users in a form. users in a form.

Example: Example: The File The File exchange.htmlexchange.html::

<html><head><title>Exchange Bank</title></head><html><head><title>Exchange Bank</title></head> <body> <h2>Exchange Bank</h2><body> <h2>Exchange Bank</h2> <form action="exchange.php">Enter value in kroner:<form action="exchange.php">Enter value in kroner: <p><input name="kroner" /></p><p><input name="kroner" /></p> <p><input type="submit" value="Get Dollar Amount“ /><p><input type="submit" value="Get Dollar Amount“ /> </p></p> </form></form> </body></body></html></html>

Page 27: CSC 405: Web Application Engineering II 2.1 Web Programming with PHP Introduction to Web programming Introduction to Web programming The programming language

CSC 405: Web Application Engineering II 2.27

Use of Form VariablesUse of Form Variables The File The File exchange.phpexchange.php::

<html><head><title>Exchange Bank</title></head><html><head><title>Exchange Bank</title></head> <body> <h2>Exchange Bank</h2><body> <h2>Exchange Bank</h2> <? $rate = 8.43; $fee = 20.0;<? $rate = 8.43; $fee = 20.0; $dollars = ($kroner - $fee) / $rate;$dollars = ($kroner - $fee) / $rate; $dollars = number_format($dollars, 2, ",", ".");$dollars = number_format($dollars, 2, ",", "."); echo "For DKr. $kroner you receive \$$dollars"; ?>echo "For DKr. $kroner you receive \$$dollars"; ?> <p><a href="exchange.html">New Computation</a><p><a href="exchange.html">New Computation</a> </body></body></html></html>

What problems does this exchange service have? Is the service What problems does this exchange service have? Is the service robust?robust?

Page 28: CSC 405: Web Application Engineering II 2.1 Web Programming with PHP Introduction to Web programming Introduction to Web programming The programming language

CSC 405: Web Application Engineering II 2.28

When a PHP script (a file with extension .php) is stored in a When a PHP script (a file with extension .php) is stored in a user’s folder user’s folder

H:\public_html\test.php H:\public_html\test.php

The script is executed when a client requests the file relative The script is executed when a client requests the file relative to the user’s home page:to the user’s home page:

http://www.itu.dk/people/user/test.php

Exercises — Problem Set 2Exercises — Problem Set 2 Temperature Conversion Multiplication Service Apple Pie Service

PHP scripts on the Web server at the ITUPHP scripts on the Web server at the ITU

Page 29: CSC 405: Web Application Engineering II 2.1 Web Programming with PHP Introduction to Web programming Introduction to Web programming The programming language

CSC 405: Web Application Engineering II 2.29

Next LectureNext Lecture

We continue with PHP:We continue with PHP: Technologies for Web sites that are programs for-loops Built-in PHP functions User defined functions Reuse of code