PHP code is embedded in HTML code with the use of The PHP command echo sends its argument (a character string "...") to the browser 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: Hello World Hi There, Greetings from a simple PHP script"> PHP code is embedded in HTML code with the use of The PHP command echo sends its argument (a character string "...") to the browser 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: Hello World Hi There, Greetings from a simple PHP script">
Download presentation
Presentation is loading. Please wait.
1
27-06-2015Databasestøttet Web-publicering1 Lektion 2: Web-programmering med PHP Introduktion til Web-programmering Programmeringssproget PHP — det første script Indlejring af kode og kommentarer Variable og udregning med tal Noget om tegn-strenge (character strings) Betingede sætninger — if-statements Løkker — while-loops Brug af form variable PHP scripts på Web-serveren
2
27-06-2015Databasestøttet Web-publicering2 Introduktion til WEB-programmering Oversigt: Klient (browser) request’er en side fra en Web-server Web-serveren udfører prgrammet Web-server-programmet tilgår (f.eks.) en database på Webserver- maskinen Resultatet af kørslen (HTML kode) sendes til klienten HTTP er protokollen med hvilken data sendes mellem en klient og en 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#,...
3
27-06-2015Databasestøttet Web-publicering3 Web Programming with PHP PHP is a programming language for Web programming Example — hello.php : Hello World Hi There, "; echo " Greetings from a simple PHP script "; ?> PHP code is embedded in HTML code with the use of The PHP command echo sends its argument (a character string "...") to the browser 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: Hello World Hi There, Greetings from a simple PHP script
4
27-06-2015Databasestøttet Web-publicering4 Code Embeddings and Comments The following example illustrates three ways of embedding code in a PHP document — embed.php: Embeddings Tree forms for embedding PHP in HTML The simple form "; ?> A slightly longer form "; ?> // Comments in PHP code is not sent to the browser echo " The somewhat longer form "; Usually, we shall use the simple form Comments that extend over several lines can be written /*... */
5
27-06-2015Databasestøttet Web-publicering5 Variables in PHP Variables are used for storing values during the execution of a PHP script 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 $ Example — homepage.php: Home page <? $name = "Martin Feldman"; echo "Home page for "; echo $name; ?> This page is maintained by /* Notice that a variable can be referred to more than once after it has been initialized with a value. */
6
27-06-2015Databasestøttet Web-publicering6 Computation with Numbers 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. Example — dollars.php : Dollars Dollars <? $rate = 8.43; $kroner = 1000.0; $dollars = ($kroner - 20.0) / $rate; echo "For DKr. $kroner you receive \$$dollars"; ?> // 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
7
27-06-2015Databasestøttet Web-publicering7 Arithmetic Operations and Evaluation Order (precedence) 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. When operators have the same precedence (same degree of binding), evaluation goes from left to right. OperatorBetydningEksempler: *Multiplikation1,5 * 60 24 * 60 /Division 134.0 / 60 134 / 60 %Modulo (rest) — 134 % 60 +Addition 1.1 + 60 14 + 60 -Subtraktion 1.1 - 60 134 - 60
8
27-06-2015Databasestøttet Web-publicering8 Example: What is the type and value of the following expressions? 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? 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. 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. Aritmetrisk udtrykResultattypeResultat 1.5 * 60 150.0 / 60 150 / 60 134 % 60 1.1 + 60 1.1 – 60
9
27-06-2015Databasestøttet Web-publicering9 More About Character Strings Character strings in PHP can be expressed either by "..." or by ’...’. In character strings on the form ’...’, variables cannot be referred to. Example: If the variable $name contains the value Per, the two statements 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 "...": \\ : backslash \n : newline \t : tabulator \" : double quote
10
27-06-2015Databasestøttet Web-publicering10 Appending of Character Strings Character strings can be appended in PHP by using the character ‘.’ Example — strings.php: Character Strings <? $firstname = "Martin"; $lastname = "Elsman"; $name = $firstname. " ". $lastname; echo "My name is $name"; ?>
11
27-06-2015Databasestøttet Web-publicering11 Conditional Statements in PHP If-statements are used for executing different code depending on some condition Example — account.php : Account <? $dollars = 8; if ( $dollars == 1 ) { echo "You have 1 Dollar on you account"; // if-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) If ($dollars == 1) is FALSE (value 0), the else-branch is executed. Otherwise the if-branch is executed. Other condition operators = = : < less than <= less than or equal > larger than >= larger than or equal != not equal
12
27-06-2015Databasestøttet Web-publicering12 The If-statement in PHP The general format: if ( condition1 ) { statement1 } elseif ( condition2 ) { statement2 } else { statement3 } //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. It is possible to omit the closing else-branch.
13
27-06-2015Databasestøttet Web-publicering13 Example: Body Mass Index — bmi.php Body Mass Index Body Mass Index <? $weight = 70.0; $height = 180.0; echo "Weight: $weight kg. Height: $height cm. "; $bmi = $weight / (($height/100.0) * ($height/100.0)); echo "Your BMI is $bmi "; if ( $bmi < 20.0 ) { echo "Your BMI is too low."; } elseif ( $bmi < 25.0 ) { echo "Your BMI is normal."; } else { echo "Your BMI is too high."; } ?>
14
27-06-2015Databasestøttet Web-publicering14 Comparison Operators and their Precedence The arithmetic operators *, /, %, +, - bind stronger than the comparison operators, >=. The comparison operators, >= bind stronger than == and !=. OperatorBetydningEksempel <Mindre end $x < 60 =<Mindre end eller lig med$x <= 60 > Større end$x > 60 >=Større end eller lig med$x >= 60 == Lig med $x = = 60 !=Forskellig fra$x != 60
15
27-06-2015Databasestøttet Web-publicering15 Example Lad x ha’ værdien 2 og y ha’ værdien 4. Husk: Tallet 1 betyder TRUE og = betyder FALSE. Logisk UdtrykResultat værdi 1 == 1 $x != $y $x < 3 + $y ($x + $y > 3) == 0 0 != $x < 3 $x == $y == 0
16
27-06-2015Databasestøttet Web-publicering16 Logical Operators and their Precedence The operator ! binds stronger than &&, which binds stronger than ||. ! also binds stronger 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. If exp1 is FALSE in exp1 && exp2 then exp2 is not evaluated. If exp1 is TRUE in exp1 || exp2 then exp2 is not evaluated. OperatorBetydningEksempel !Ikke-negation-NOT !($x == 60) &&Og-konjunktion-AND 0 <= $x && $x < 60 || Eller-disjunktion-OR $x = 60
17
27-06-2015Databasestøttet Web-publicering17 Example: Hvad er resultatet af nedenstående logiske udtryk? Lad x = 2 og y = 4. Boolske udtrykResultatværdi ! 0 ! 1 ! 1 == 0 ! (1 == 0) 1 && 0 0 || 1 $x + $y > 3 && $x < $y $x + $y == 3 || $x < 4
18
27-06-2015Databasestøttet Web-publicering18 Løkker i PHP Hvordan kan vi skrive teksten "I love Web programming" 20 gange ? Dårlig løsning : echo "I love Web programming ";...18 gange... echo "I love Web programming "; Bedre løsning: anvend en while-løkke til repetition — love.php: $counter = 20; while ( $counter >= 1 ) { echo "I love Web programming "; $counter = $counter - 1; } Sætningen echo udføres 20 gange, med $counter = 20, 19,..., 1 Det er vigtigt at indholdet af variablen $counter (i.e., et tal ) aftager (nedtælles) for hver udførelse af løkkens krop. Hvad sker der hvis variablen $counter ikke tælles ned?
19
27-06-2015Databasestøttet Web-publicering19 Syntax for while-løkker Generelt format: while ( condition ) { statement ; } Betydning: 1.Evaluering af betingelsen condition 2.Hvis resultatet er forskelligt fra 0 (i.e., TRUE), så udfør statement, og fortsæt ved (1) Således vil kroppen body i while-løkken (statement) udføres så længe betingelsen (condition) er TRUE Ofte anvendt while-konstruktion — love.php: initialization ; while ( condition ) { statement ; increment ; // alternativ optælling }
20
27-06-2015Databasestøttet Web-publicering20 Examples of while-loops — loops1.php Standard loop, where $i takes the values __, __, __, __, __, __, __, __, __, __, __: $i = 10; while ( $i >= 1 ) { $i = $i - 1; } echo "Loop Example 1: $i "; Standard loop, where $i takes the values ___, ___, ___, ___, ___, ___: $i = 1; while ( $i <= 10 ) { $i = $i + 2; } echo "Loop Example 2: $i "; Standard loop, where $i takes the values ___, ___, ___, ___, ___, ___, ___, ___: $i = 1; while ( $i <= 100 ) { $i = $i * 2; } echo "Loop Example 3: $i "; What is the value of $i after each loop?
21
27-06-2015Databasestøttet Web-publicering21 Loop Exercises with while — loops2.php Write a while-loop that outputs 64, 32, 16, 8, 4, 2, 1: $i = __ ; while ( __ >= __ ) { echo "$i, "; $i = __ / __ ; } Write a while-loop that outputs 2, 4, 6, 8,..., 100: $i = __ ; while ( __ <= __ ) { echo "$i, "; $i = $i + __ ; } Write a while-loop that outputs 100, 110, 120,..., 200: $i = __ ; while ( $i <= __ ) { echo "$i, "; $i = $i + __ ; }
22
27-06-2015Databasestøttet Web-publicering22 Anvendelse af Form Variable Det er muligt for PHP at anvende data, fra brugere via HTML form. Eks.: The File exchange.html: Exchange Bank Enter value in kroner: The File exchange.php: Exchange Bank <? $rate = 8.43; $fee = 20.0; $dollars = ($kroner - $fee) / $rate; $dollars = number_format($dollars, 2, ",", "."); echo "For DKr. $kroner you receive \$$dollars"; ?> New Computation What problems does this exchange service have? Is the service robust?
23
27-06-2015Databasestøttet Web-publicering23 PHP scripts på Web-serveren ved ITU Når et PHP script (en fil med efternavn eller extension.php) gemmes i en brugers/studerendes mappe (folder) H:\public_html\test.php Vil scriptet blive udført, når en klient request’er filen relateret til brugerens hjemmeside: http://www.itu.dk/people/user/test.php Øvelser — Problem Set 2 Temperature Conversion Multiplication Service Apple Pie Service
24
27-06-2015Databasestøttet Web-publicering24 Næste Lektion Næste lektion (lektion 3 mandag den 12/9) Vi fortsætter med PHP: Teknologier for Web-sites som er programmer for-løkker Indbyggede PHP funktioner Bruger-definerede funktioner Genbrug af kode
Similar presentations
© 2024 SlidePlayer.com. Inc.
All rights reserved.