Download presentation
Presentation is loading. Please wait.
Published byCuthbert Crawford Modified over 9 years ago
1
CSC 405: Web Application Engineering II 2.1 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
2
CSC 405: Web Application Engineering II 2.2 Introduction to Web Programming OVERVIEW: Client (browser) requests a page on a Web server Client (browser) requests a page on a Web server Web server executes the program Web server executes the program The Web server program accesses (for instance) a database on the Web server machine The Web server program accesses (for instance) a database on the Web server machine The result of the execution (HTML code) is sent to the client The result of the execution (HTML code) is sent to the client
3
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#,...
4
CSC 405: Web Application Engineering II 2.4 Web Programming with PHP PHP is a programming language for Web programming Example — hello.php: Hello World Hello World Hi There, "; Hi There, "; echo " Greetings from a simple PHP script "; ?> echo " Greetings from a simple PHP script "; ?> </html>
5
CSC 405: Web Application Engineering II 2.5 Web 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 The PHP command echo sends its argument (a character string "...") to the browser The PHP command echo sends its argument (a character string "...") 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, the PHP code is executed, which results in HTML code, which again is sent to the browser: When a browser requests the page hello.php on the Web server, the PHP code is executed, which results in HTML code, which again is sent to the browser: Hello World Hello World Hi There, Greetings from a simple PHP script Hi There, Greetings from a simple PHP script </html>
6
CSC 405: Web Application Engineering II 2.6 Code Embeddings and Comments The following example illustrates three ways of embedding code in a PHP document — embed.php: The following example illustrates three ways of embedding code in a PHP document — embed.php: Embeddings Embeddings Tree forms for embedding PHP in HTML Tree forms for embedding PHP in HTML The simple form "; ?> The simple form "; ?> A slightly longer form "; ?> A slightly longer form "; ?> // Comments in PHP code is not sent to the browser // Comments in PHP code is not sent to the browser echo " The somewhat longer form "; </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 /*... */
7
CSC 405: Web Application Engineering II 2.7 Variables in PHP Variables are used for storing values during the execution of a PHP script Variables are used for storing values during the execution of a PHP script Values can, for instance, be character strings and numbers Values can, for instance, be character strings and numbers Names of variables are chosen freely by the programmer, although variable names must start with the character $ Names of variables are chosen freely by the programmer, although variable names must start with the character $ Example — homepage.php: Example — homepage.php: Home page Home page <? $name = "Martin Elsman"; echo "Home page for "; echo "Home page for "; echo $name; echo $name; ?> ?> This page is maintained by This page is maintained by Notice that a variable can be referred to more than once after it has been initialized with a value. Notice that a variable can be referred to more than once after it has been initialized with a value.
8
CSC 405: Web Application Engineering II 2.8 Computation 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 computations. The usual number operations (e.g., +,-,*, and /) can be used in computations. Example — dollars.php: Example — dollars.php: Dollars Dollars Dollars Dollars <? $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"; ?> 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 placed before the dollar-sign To insert a dollar-sign ($) in a character string, the character \ should be placed before the dollar-sign
9
CSC 405: Web Application Engineering II 2.9 Arithmetic Operations and Evaluation Order (precedence) Operators with a high precedence bind stronger than operators with a low precedence, and are therefore evaluated first. Operators with a high precedence bind stronger than operators with a low precedence, and are therefore evaluated first. The operators *, /, and % have higher precedence than + and -, thus operations with these operators are evaluated first. The operators *, /, and % have higher precedence than + and -, thus operations with these operators are evaluated first. When operators have the same precedence (same degree of binding), evaluation goes from left to right. When operators have the same precedence (same degree of binding), evaluation goes from left to right.
10
CSC 405: Web Application Engineering II 2.10 Arithmetic Operations and Evaluation Order (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.924 Without precedence rules, expressions can be ambiguous: Does 2+4*4 evaluate to 24 or 18? Without precedence rules, expressions can be ambiguous: Does 2+4*4 evaluate to 24 or 18? Because * has a higher precedence than +, 4*4 is evaluated first whereafter 2 is added. Because * has a higher precedence than +, 4*4 is evaluated first whereafter 2 is added. To evaluate the addition first, parentheses can be used: The expression (2+4)*4 evaluates to 24. To evaluate the addition first, parentheses can be used: The expression (2+4)*4 evaluates to 24. Because and = have the same precedence, 2*5/2 is evaluated from left to right, resulting in the value 5. Because and = have the same precedence, 2*5/2 is evaluated from left to right, resulting in the value 5. To evaluate the division first, parentheses can be used: The expression 2*(5/2) evaluates to the value 4. To evaluate the division first, parentheses can be used: The expression 2*(5/2) evaluates to the value 4.
11
CSC 405: Web Application Engineering II 2.11 More 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 statements Example: If the variable $name contains the value Per, the two statements echo "Your name is $name"; echo "Your name is $name"; echo ’Your name is $name’; echo ’Your name is $name’; result 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
12
CSC 405: Web Application Engineering II 2.12 Appending 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 — strings.php: Example — strings.php: Character Strings Character Strings <? $firstname = "Martin"; <? $firstname = "Martin"; $lastname = "Elsman"; $lastname = "Elsman"; $name = $firstname. " ". $lastname; $name = $firstname. " ". $lastname; echo "My name is $name"; echo "My name is $name"; ?> ?> </html>
13
CSC 405: Web Application Engineering II 2.13 Conditional Statements in PHP If-statements are used for executing different code depending on some condition If-statements are used for executing different code depending on some condition Example — account.php: Example — account.php: Account Account <? $dollars = 8; <? $dollars = 8; if ( $dollars == 1 ) { echo "You have 1 Dollar on you account"; // if-branch if ( $dollars == 1 ) { echo "You have 1 Dollar on you account"; // if-branch } else { echo "You have $dollars Dollars on your account"; // else-branch } else { echo "You have $dollars Dollars on your account"; // else-branch } ?> ?> 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) Other condition operators Other condition operators larger than, != different from, = larger than or equal
14
CSC 405: Web Application Engineering II 2.14 The If-statement in PHP The general format: The general format: if ( condition1 ) { if ( condition1 ) { statement1 statement1 } elseif ( condition2 ) { } elseif ( condition2 ) { statement2 statement2 } else { } else { statement3 statement3 } 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 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.
15
CSC 405: Web Application Engineering II 2.15 The If-statement in PHP Example: Body Mass Index — bmi.php Example: Body Mass Index — bmi.php Body Mass Index Body Mass Index Body Mass Index Body Mass Index <?php $weight = 70.0; $height = 180.0; <?php $weight = 70.0; $height = 180.0; echo "Weight: $weight kg. Height: $height cm. "; echo "Weight: $weight kg. Height: $height cm. "; $bmi = $weight / (($height/100.0) * ($height/100.0)); $bmi = $weight / (($height/100.0) * ($height/100.0)); echo "Your BMI is $bmi "; echo "Your BMI is $bmi "; 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."; } ?> ?>
16
CSC 405: Web Application Engineering II 2.16 Comparison Operators and their Precedence The arithmetic operators *, /, %, +, - bind stronger than the comparison operators, >=. The arithmetic operators *, /, %, +, - bind stronger than the comparison operators, >=. The comparison operators, >= bind stronger than == and !=. The comparison operators, >= bind stronger than == and !=.
17
CSC 405: Web Application Engineering II 2.17 Comparison Operators and their Precedence Example Let x have the value 2 and y have the value 4. Remember: The number 1 denotes TRUE and 0 denotes FALSE.
18
CSC 405: Web Application Engineering II 2.18 Logical 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 arithmetic operators. ! also binds stronger than the comparison operators and the arithmetic operators. && and || bind weaker than the comparison operators and the arithmetic operators. && and || bind weaker than the comparison operators and the arithmetic operators. Evaluation goes from left to right. Evaluation goes from left to right. If exp1 is FALSE in exp1 && exp2 then exp2 is not evaluated. If exp1 is FALSE in exp1 && exp2 then exp2 is not evaluated. If exp1 is TRUE in exp1 || exp2 then exp2 is not evaluated. If exp1 is TRUE in exp1 || exp2 then exp2 is not evaluated.
19
CSC 405: Web Application Engineering II 2.19 Logical Operators and their Precedence Example: What is the result of the following logical expressions? Let x = 2 and y = 4. Remember: the value 1 denotes TRUE and 0 denotes FALSE.
20
CSC 405: Web Application Engineering II 2.20 Loops in PHP How can we output the text "I love Web programming" 20 times? Bad solution: echo "I love Web programming "; echo "I love Web programming ";...18 times......18 times... echo "I love Web programming "; echo "I love Web programming "; 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 "; E cho "I love Web programming "; $counter = $counter - 1; $counter = $counter - 1; }
21
CSC 405: Web Application Engineering II 2.21 Loops in PHP The statement echo is executed 20 times, with $counter = 20, 19,..., 1 The statement echo is executed 20 times, with $counter = 20, 19,..., 1 It is important that the content of the variable $counter (i.e., a number) decreases for each execution of the loop-body. It is important that the content of the variable $counter (i.e., a 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-loops Syntax for while-loops while ( condition ) { while ( condition ) { statement ; statement ; } Meaning: Meaning: 1. Evaluate the condition 2. If the result is different from 0 (i.e., TRUE), then evaluate statement, and continue at (1)
22
CSC 405: Web Application Engineering II 2.22 Loops in PHP Thus, the body of the while-loop (statement) is evaluated as long as the condition is TRUE Thus, the body of the while-loop (statement) is evaluated as long as the condition is TRUE Often used while-construction — love.php : Often used while-construction — love.php : initialization ; initialization ; while ( condition ) { while ( condition ) { statement ; statement ; increment ; increment ; } Examples of while-loops — loops1.php Examples of while-loops — loops1.php 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 "; echo "Loop Example 1: $i ";
23
CSC 405: Web Application Engineering II 2.23 Loops 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 "; echo "Loop Example 2: $i "; 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 "; echo "Loop Example 3: $i "; What is the value of $i after each loop? What is the value of $i after each loop?
24
CSC 405: Web Application Engineering II 2.24 Loops in PHP Loop Exercises with while — loops2.php Loop Exercises with while — loops2.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 + __ ; }
25
CSC 405: Web Application Engineering II 2.25 Loops 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 + __ ; }
26
CSC 405: Web Application Engineering II 2.26 Use 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: The File exchange.html: Example: The File exchange.html: Exchange Bank Exchange Bank Enter value in kroner: Enter value in kroner: </html>
27
CSC 405: Web Application Engineering II 2.27 Use of Form Variables The File exchange.php : The File exchange.php : Exchange Bank Exchange Bank <? $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"; ?> New Computation New Computation </html> What problems does this exchange service have? Is the service robust? What problems does this exchange service have? Is the service robust?
28
CSC 405: Web Application Engineering II 2.28 When a PHP script (a file with extension.php) is stored in a user’s folder When a PHP script (a file with extension.php) is stored in a user’s folderH:\public_html\test.php The script is executed when a client requests the file relative to the user’s home page: The script is executed when a client requests the file relative to the user’s home page: http://www.itu.dk/people/user/test.php Exercises — Problem Set 2 Exercises — Problem Set 2 Temperature Conversion Multiplication Service Apple Pie Service PHP scripts on the Web server at the ITU
29
CSC 405: Web Application Engineering II 2.29 Next 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
Similar presentations
© 2024 SlidePlayer.com. Inc.
All rights reserved.