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

29
CSC 405: Web Application And 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: matilda-ellis

Post on 26-Dec-2015

224 views

Category:

Documents


0 download

TRANSCRIPT

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

CSC 405: Web Application And 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 And Engineering II 2.1 Web Programming with PHP Introduction to Web programming Introduction to Web programming The programming

CSC 405: Web Application And 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 And Engineering II 2.1 Web Programming with PHP Introduction to Web programming Introduction to Web programming The programming

CSC 405: Web Application And 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, Erlang, Haskell, C#, ...PHP, Scheme, Lisp, Standard ML, Erlang, Haskell, C#, ...

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

CSC 405: Web Application And 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 And Engineering II 2.1 Web Programming with PHP Introduction to Web programming Introduction to Web programming The programming

CSC 405: Web Application And Engineering II 2.5

Web Programming with PHPWeb Programming with PHP

PHP code is embedded in HTML code with the use of <? and ?>PHP code is embedded in HTML code with the use of <? and ?> The PHP command echo sends its argument (a character string The PHP command echo sends its argument (a character string

"...") to the browser"...") to the browser PHP commands are ended with the character ;PHP commands are ended with the character ; When a browser requests the page hello.php on the Web server, When a browser requests the page hello.php on the Web server,

the PHP code is executed, whichthe PHP code is executed, which results in HTML code, which again is sent to the browser:results in 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 PHP script</p><p>Hi There, </p><p>Greetings from a simple PHP script</p> </body></body></html></html>

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

CSC 405: Web Application And Engineering II 2.6

Code Embeddings and CommentsCode Embeddings and Comments

The following example illustrates three ways of embedding code in a The following example illustrates three ways of embedding code in a PHP document — embed.php:PHP document — embed.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 And Engineering II 2.1 Web Programming with PHP Introduction to Web programming Introduction to Web programming The programming

CSC 405: Web Application And Engineering II 2.7

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

scriptscriptValues can, for instance, be character strings and numbersValues can, for instance, be character strings and numbersNames of variables are chosen freely by the programmer, although Names of variables are chosen freely by the programmer, although

variable names must start with the character $variable names must start with the character $Example — homepage.php:Example — homepage.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 Notice that a variable can be referred to more than once after it has referred to more than once after it has

been initialized with a value.been initialized with a value.

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

CSC 405: Web Application And Engineering II 2.8

Computation with NumbersComputation with NumbersThere are two types of numbers in PHP:There are two types of numbers in PHP:1. Integers: 3, 9, 0, -1, ...1. Integers: 3, 9, 0, -1, ...2. Doubles (double precision): 3.0, 9.2, 0.13, -23.2, ...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. Example — dollars.php:computations. Example — dollars.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 \ should be To insert a dollar-sign ($) in a character string, the character \ should be

placed before the dollar-signplaced before the dollar-sign

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

CSC 405: Web Application And 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 thereforea low precedence, and are therefore

evaluated first.evaluated first. The operators *, /, and % have higher precedence than + and -, The operators *, /, and % have higher precedence than + and -,

thus operations with these operators arethus operations with these operators are evaluated first.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 And Engineering II 2.1 Web Programming with PHP Introduction to Web programming Introduction to Web programming The programming

CSC 405: Web Application And 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 instance:It is possible to construct arbitrarily complicated expressions, for 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 whereafter Because * has a higher precedence than +, 4*4 is evaluated first whereafter

2 is added.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 to Because and = have the same precedence, 2*5/2 is evaluated from left to

right, resulting in the value 5.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 And Engineering II 2.1 Web Programming with PHP Introduction to Web programming Introduction to Web programming The programming

CSC 405: Web Application And 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 inYour name is PerYour name is Per Your name is $nameYour name is $nameOther Other special characters in character strings on the form "...":special characters in character strings on the form "...": \\ : backslash\\ : backslash \n : newline\n : newline \t : tabulator\t : tabulator \" : double quote\" : double quote

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

CSC 405: Web Application And Engineering II 2.12

Appending of Character StringsAppending of Character Strings

Character strings can be appended in PHP by using the Character strings can be appended in PHP by using the character ‘.’character ‘.’

Example — strings.php:Example — strings.php: <<html><head><title>Character html><head><title>Character

Strings</title></head>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 And Engineering II 2.1 Web Programming with PHP Introduction to Web programming Introduction to Web programming The programming

CSC 405: Web Application And Engineering II 2.13

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

conditionconditionExample — account.php:Example — account.php: <html><head><title>Account</title></head><html><head><title>Account</title></head> <body><body> <? $dollars = 8;<? $dollars = 8; if ( $dollars == 1 ) { if ( $dollars == 1 ) { echo "You have 1 Dollar on you account"; // if-branchecho "You have 1 Dollar on you account"; // if-branch } } else { else { echo "You have $dollars Dollars on your account"; // else-branchecho "You have $dollars Dollars on your account"; // else-branch }} ?>?> </body></html></body></html>A condition is either FALSE (the value 0) or TRUE (different from 0)A condition is either FALSE (the value 0) or TRUE (different from 0)If ($dollars == 1) is FALSE (value 0), the else-branch is executed. Otherwise the if-branch If ($dollars == 1) is FALSE (value 0), the else-branch is executed. Otherwise the if-branch

is executed.is executed.Other condition operators == :Other condition operators == : < less than > larger than != different from< less than > larger than != different from <= less than or equal >= larger than or equal<= less than or equal >= larger than or equal

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

CSC 405: Web Application And 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 1. compute the value of condition1.condition1. 2. if different from 0 (i.e., TRUE) then execute 2. if different from 0 (i.e., TRUE) then execute statement1, otherwisestatement1, otherwise 3. compute the value of 3. compute the value of condition2condition2 4. if different from 0 (i.e., TRUE) then execute 4. if different from 0 (i.e., TRUE) then execute statement2, otherwisestatement2, otherwise 5. execute 5. execute statement3statement3An arbitrary number of elseif-branches can be used.An arbitrary number of elseif-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 And Engineering II 2.1 Web Programming with PHP Introduction to Web programming Introduction to Web programming The programming

CSC 405: Web Application And Engineering II 2.15

The If-statement in PHPThe If-statement in PHP Example: Body Mass Index — bmi.phpExample: Body Mass Index — bmi.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 And Engineering II 2.1 Web Programming with PHP Introduction to Web programming Introduction to Web programming The programming

CSC 405: Web Application And 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 And Engineering II 2.1 Web Programming with PHP Introduction to Web programming Introduction to Web programming The programming

CSC 405: Web Application And 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 And Engineering II 2.1 Web Programming with PHP Introduction to Web programming Introduction to Web programming The programming

CSC 405: Web Application And 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 And Engineering II 2.1 Web Programming with PHP Introduction to Web programming Introduction to Web programming The programming

CSC 405: Web Application And 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 And Engineering II 2.1 Web Programming with PHP Introduction to Web programming Introduction to Web programming The programming

CSC 405: Web Application And 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 — love.php:Better solution: use a while-loop for repetition — love.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 And Engineering II 2.1 Web Programming with PHP Introduction to Web programming Introduction to Web programming The programming

CSC 405: Web Application And Engineering II 2.21

Loops in PHPLoops in PHP

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

It is important that the content of the variable $counter (i.e., a It is important that the content of the variable $counter (i.e., a number) decreases for each execution ofnumber) decreases for each execution of

the loop-body.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 General format:General format: while ( while ( condition ) {condition ) { statement ;statement ; }} Meaning:Meaning: (1) Evaluate the (1) Evaluate the conditioncondition (2) If the result is different from 0 (i.e., TRUE), then evaluate (2) If the result is different from 0 (i.e., TRUE), then evaluate

statement, and continue at (1)statement, and continue at (1)

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

CSC 405: Web Application And 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 — love.php:Often used while-construction — love.php: initialization ;initialization ; while ( while ( condition ) {condition ) { statement ;statement ; increment ;increment ; }}Examples of while-loops — loops1.phpExamples of while-loops — loops1.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 And Engineering II 2.1 Web Programming with PHP Introduction to Web programming Introduction to Web programming The programming

CSC 405: Web Application And 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 And Engineering II 2.1 Web Programming with PHP Introduction to Web programming Introduction to Web programming The programming

CSC 405: Web Application And Engineering II 2.24

Loops in PHPLoops in PHP

Loop Exercises with while — loops2.phpLoop Exercises with while — loops2.phpWrite 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 And Engineering II 2.1 Web Programming with PHP Introduction to Web programming Introduction to Web programming The programming

CSC 405: Web Application And 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 And Engineering II 2.1 Web Programming with PHP Introduction to Web programming Introduction to Web programming The programming

CSC 405: Web Application And Engineering II 2.26

Use of Form VariablesUse of Form Variables

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

The File exchange.html:The File exchange.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 type=”text” name="kroner" /></p><p><input type=”text” 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 And Engineering II 2.1 Web Programming with PHP Introduction to Web programming Introduction to Web programming The programming

CSC 405: Web Application And Engineering II 2.27

Use of Form VariablesUse of Form Variables

The File exchange.php:The File exchange.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 robust?What problems does this exchange service have? Is the service robust?

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

CSC 405: Web Application And Engineering II 2.28

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

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

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

http://www.itu.dk/people/user/test.php Exercises — Problem Set 2Exercises — Problem Set 2 Temperature ConversionTemperature Conversion Multiplication ServiceMultiplication Service Apple Pie ServiceApple Pie Service

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

CSC 405: Web Application And Engineering II 2.29

Next LectureNext Lecture

We continue with PHP:We continue with PHP: Technologies for Web sites that are programsTechnologies for Web sites that are programs for-loopsfor-loops Built-in PHP functionsBuilt-in PHP functions User defined functionsUser defined functions Reuse of codeReuse of code WeWe