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

Slides:



Advertisements
Similar presentations
PHP I.
Advertisements

Session 1 & 2BBK P1 Module5-May-2007 : [‹#›] PHP: The Basics.
JavaScript FaaDoOEngineers.com FaaDoOEngineers.com.
Introduction to PHP MIS 3501, Fall 2014 Jeremy Shafer
1 PHP Statement Constructs Server Scripting. 5-2 Basic Statement All Statements end in a semicolon. Statements are delimited from the HTML code by enclosing.
IST 221 Internet Concepts and Applications Introduction to PHP.
Databasestøttet Web-publicering1 Lektion 2: Web-programmering med PHP Introduktion til Web-programmering Programmeringssproget PHP — det første.
Guide To UNIX Using Linux Third Edition
PHP Server-side Programming. PHP  PHP stands for PHP: Hypertext Preprocessor  PHP is interpreted  PHP code is embedded into HTML code  interpreter.
1 CS428 Web Engineering Lecture 18 Introduction (PHP - I)
PHP Workshop ‹#› PHP: The Basics. PHP Workshop ‹#› What is it? PHP is a scripting language commonly used on web servers. –Stands for “PHP: Hypertext Preprocessor”
Lecture 2 Introduction to PHP MIS 3501, Spring 2014 Jeremy Shafer Department of MIS Fox School of Business Temple University January 30, 2014.
DAT602 Database Application Development Lecture 15 Java Server Pages Part 1.
PHP: Hypertext Processor Fred Durao
ALBERT WAVERING BOBBY SENG. Week Whatever: PHP  Announcements/questions/complaints.
PHP == ‘ Hypertext Preprocessor ’ Open-source, server-side scripting language Used to generate dynamic web-pages PHP scripts reside between reserved PHP.
CSC 405: Web Application And Engineering II 2.1 Web Programming with PHP Introduction to Web programming Introduction to Web programming The programming.
Advanced Web 2012 Lecture 4 Sean Costain PHP Sean Costain 2012 What is PHP? PHP is a widely-used general-purpose scripting language that is especially.
2010/11 : [1]Building Web Applications using MySQL and PHP (W1)PHP Recap.
PHP H ypertext P re-processor. Unit 6 - PHP - Hello World! - Data types - Control structures - Operators.
Lecture Note 3: ASP Syntax.  ASP Syntax  ASP Syntax ASP Code is Browser-Independent. You cannot view the ASP source code by selecting "View source"
Week 9 PHP Cookies and Session Introduction to JavaScript.
Internet & World Wide Web How to Program, 5/e © by Pearson Education, Inc. All Rights Reserved.
IST 210: PHP BASICS IST 210: Organization of Data IST210 1.
Intro to PHP – Page 1 of 43CSCI 2910 – Client/Server-Side Programming CSCI 2910 Client/Server-Side Programming Topic: Intro to PHP Reading: Chapters 1.
November 2003Bent Thomsen - FIT 6-11 IT – som værktøj Bent Thomsen Institut for Datalogi Aalborg Universitet.
 2003 Prentice Hall, Inc. All rights reserved. CHAPTER 3 JavaScript 1.
Introduction to PHP A user navigates in her browser to a page that ends with a.php extension The request is sent to a web server, which directs the request.
Overview: 1. Discussion of the basic architecture of a web application. 2. Discussion of the relevance of using MySQL and PHP in a web application.
CSC 2720 Building Web Applications Server-side Scripting with PHP.
Just a Little PHP Programming PHP on the Server. Common Programming Language Features Comments Data Types Variable Declarations Expressions Flow of Control.
Intro to PHP IST2101. Review: HTML & Tags 2IST210.
ITM © Port, KazmanVariables - 1 ITM 352 Expressions, Precedence, Working with Strings.
PHP - 1h. How it works Client requests document Server loads document in memory Server processes document with relevant module (PHP) Server sends XHTML.
Web Scripting [PHP] CIS166AE Wednesdays 6:00pm – 9:50pm Rob Loy.
XP Tutorial 8 Adding Interactivity with ActionScript.
Server-Side Scripting with PHP ISYS 475. PHP Manual Website
Internet & World Wide Web How to Program, 5/e © by Pearson Education, Inc. All Rights Reserved.
David Lawrence 7/8/091Intro. to PHP -- David Lawrence.
Web Scripting [PHP] CIS166AE Wednesdays 6:00pm – 9:50pm Rob Loy.
IT ELECTIVE 2.  Web server Can refer to either the hardware (the computer) or the software (the computer application) that helps to deliver content that.
 2008 Pearson Education, Inc. All rights reserved JavaScript: Introduction to Scripting.
Internet & World Wide Web How to Program, 5/e © by Pearson Education, Inc. All Rights Reserved.
CSC 405: Web Application Engineering II5.1 Web programming using PHP What have we learnt? What have we learnt? Motivation for validating user input (form.
Open Source Server Side Scripting ECA 236 Open Source Server Side Scripting PHP Basics.
8 th Semester, Batch 2009 Department Of Computer Science SSUET.
1 PHP Intro PHP Introduction After this lecture, you should be able to: Know the fundamental concepts of Web Scripting Languages in general, PHP in particular.
PHP Form Processing * referenced from
Dr. Abdullah Almutairi Spring PHP is a server scripting language, and a powerful tool for making dynamic and interactive Web pages. PHP is a widely-used,
PHP Tutorial. What is PHP PHP is a server scripting language, and a powerful tool for making dynamic and interactive Web pages.
CGS 3066: Web Programming and Design Spring 2016 PHP.
REEM ALMOTIRI Information Technology Department Majmaah University.
1 Agenda  Unit 7: Introduction to Programming Using JavaScript T. Jumana Abu Shmais – AOU - Riyadh.
IST 210: PHP Basics IST 210: Organization of Data IST2101.
1 Lecture 2 - Introduction to C Programming Outline 2.1Introduction 2.2A Simple C Program: Printing a Line of Text 2.3Another Simple C Program: Adding.
PHP using MySQL Database for Web Development (part II)
Chapter 6 JavaScript: Introduction to Scripting
JavaScript Syntax and Semantics
ITM 352 Expressions, Precedence, Working with Strings Class #5
PHP Introduction.
Intro to PHP & Variables
Introduction to C++ Programming
PHP.
T. Jumana Abu Shmais – AOU - Riyadh
HYPERTEXT PREPROCESSOR BY : UMA KAKKAR
Tutorial 10: Programming with javascript
PHP: Hypertext Preprocessor
PHP an introduction.
Presentation transcript:

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

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

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#,...

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>

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>

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 /*... */

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.

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, 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 = ; $kroner = ; $dollars = ($kroner ) / $rate; $dollars = ($kroner ) / $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

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.

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: % 34 * / * evaluates to 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.

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

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>

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

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.

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."; } ?> ?>

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 !=.

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.

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.

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.

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 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; }

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)

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 ";

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?

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 + __ ; }

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 + __ ; }

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>

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?

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: Exercises — Problem Set 2 Exercises — Problem Set 2  Temperature Conversion  Multiplication Service  Apple Pie Service PHP scripts on the Web server at the ITU

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