Documenti di Didattica
Documenti di Professioni
Documenti di Cultura
Introduction to Scripting, Databases and System Architecture, Spring 2007 The IT University of Copenhagen Page 2-1
Overview:
INTERNET
WEB SERVER HTTP
DATABASE SQL BROWSER
(MySQL) (Apache)
HTTP
HTTP is the protocol with which data is sent between a client and a Web server
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#, ...
Introduction to Scripting, Databases and System Architecture, Spring 2007 The IT University of Copenhagen Page 2-2
Web Programming with PHP
<html><head><title>Hello World</title></head>
<body>
<? echo "<p>Hi There, </p>";
echo "<p>Greetings from a simple PHP script</p>"; ?>
</body>
</html>
• PHP code is embedded in HTML code with the use of <? and ?>
• 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:
<html><head><title>Hello World</title></head>
<body>
<p>Hi There, </p><p>Greetings from a simple PHP script</p>
</body>
</html>
Introduction to Scripting, Databases and System Architecture, Spring 2007 The IT University of Copenhagen Page 2-3
The following example illustrates three ways of embedding code in a PHP document — embed.php:
<html><head><title>Embeddings</title></head>
<body> <h2>Three forms for embedding PHP in HTML</h2>
<ol>
<? echo "<li>The simple form</li>"; ?>
<script language="PHP">
// Comments in PHP code is not sent to the browser
echo "<li>The somewhat longer form</li>";
</script>
</ol>
</body>
</html>
Introduction to Scripting, Databases and System Architecture, Spring 2007 The IT University of Copenhagen Page 2-4
Variables in PHP
Variables are used for storing values during the execution of a PHP script
Names of variables are chosen freely by the programmer, although variable names must start with the character $
Example — homepage.php:
<html><head><title>Home page</title></head>
<body>
<h2>
<? $name = "Martin Elsman";
echo "Home page for ";
echo $name;
?>
</h2>
<hr />
This page is maintained by
<? echo $name ?>
</body>
</html>
Notice that a variable can be referred to more than once after it has been initialized with a value.
Introduction to Scripting, Databases and System Architecture, Spring 2007 The IT University of Copenhagen Page 2-5
The usual number operations (e.g., +,-,*, and /) can be used in computations. Example — dollars.php:
<html><head><title>Dollars</title></head>
<body>
<h2>Dollars</h2>
<? $rate = 8.43;
$kroner = 1000.0;
$dollars = ($kroner - 20.0) / $rate;
echo "For DKr. $kroner you receive \$$dollars"; ?>
</body>
</html>
Notice:
Introduction to Scripting, Databases and System Architecture, Spring 2007 The IT University of Copenhagen Page 2-6
Arithmetic Operations and Evaluation Order (precedence)
* Multiplication 1.5 * 60 24 * 60
/ Division 134.0 / 60 134 / 60
% Remainder — 134 % 60
+ Addition 1.1 + 60 14 + 60
- Subtraction 1.1 - 60 134 - 60
• 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.
Introduction to Scripting, Databases and System Architecture, Spring 2007 The IT University of Copenhagen Page 2-7
1.5 * 60
150.0 / 60
150 / 60
134 % 60
1.1 + 60
1.1 - 60
• 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.
Introduction to Scripting, Databases and System Architecture, Spring 2007 The IT University of Copenhagen Page 2-8
More About Character Strings
Example: If the variable $name contains the value Per, the two statements
result in
• \\ : backslash
• \n : newline
• \t : tabulator
• \" : double quote
Introduction to Scripting, Databases and System Architecture, Spring 2007 The IT University of Copenhagen Page 2-9
Example — strings.php:
<html><head><title>Character Strings</title></head>
<body>
<? $firstname = "Martin";
$lastname = "Elsman";
$name = $firstname . " " . $lastname;
echo "My name is $name";
?>
</body>
</html>
Introduction to Scripting, Databases and System Architecture, Spring 2007 The IT University of Copenhagen Page 2-10
Conditional Statements in PHP
If-statements are used for executing different code depending on some condition
Example — account.php:
<html><head><title>Account</title></head>
<body>
<? $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
}
?>
</body>
</html>
If ($dollars == 1) is FALSE (value 0), the else-branch is executed. Otherwise the if-branch is executed.
Introduction to Scripting, Databases and System Architecture, Spring 2007 The IT University of Copenhagen Page 2-11
Introduction to Scripting, Databases and System Architecture, Spring 2007 The IT University of Copenhagen Page 2-12
Example: Body Mass Index — bmi.php
Introduction to Scripting, Databases and System Architecture, Spring 2007 The IT University of Copenhagen Page 2-13
• The arithmetic operators *, /, %, +, - bind stronger than the comparison operators <, <=, >, >=.
• The comparison operators <, <=, >, >= bind stronger than == and !=.
Introduction to Scripting, Databases and System Architecture, Spring 2007 The IT University of Copenhagen Page 2-14
Example
1 == 1
$x != $y
$x < 3 + $y
($x + $y > 3) == 0
0 != $x < 3
$x == $y == 0
Introduction to Scripting, Databases and System Architecture, Spring 2007 The IT University of Copenhagen Page 2-15
• 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.
Introduction to Scripting, Databases and System Architecture, Spring 2007 The IT University of Copenhagen Page 2-16
Example: What is the result of the following logical expressions?
Let x = 2 and y = 4.
! 0
! 1
! 1 == 0
! (1 == 0)
1 && 0
0 || 1
$x + $y > 3 && $x < $y
$x + $y == 3 || $x < 4
Introduction to Scripting, Databases and System Architecture, Spring 2007 The IT University of Copenhagen Page 2-17
Loops in PHP
How can we output the text "I love Web programming" 20 times?
Bad solution:
$counter = 20;
while ( $counter >= 1 ) {
echo "I love Web programming<br />";
$counter = $counter - 1;
}
Introduction to Scripting, Databases and System Architecture, Spring 2007 The IT University of Copenhagen Page 2-18
Syntax for while-loops
General format:
while ( condition ) {
statement ;
}
Meaning:
(2) If the result is different from 0 (i.e., TRUE), then evaluate statement, and continue at (1)
Thus, the body of the while-loop (statement) is evaluated as long as the condition is TRUE
initialization ;
while ( condition ) {
statement ;
increment ;
}
Introduction to Scripting, Databases and System Architecture, Spring 2007 The IT University of Copenhagen Page 2-19
Standard loop, where $i takes the values ___, ___, ___, ___, ___, ___, ___, ___, ___, ___, ___:
$i = 10;
while ( $i >= 1 ) {
$i = $i - 1;
}
echo "Loop Example 1: $i <br />";
Standard loop, where $i takes the values ___, ___, ___, ___, ___, ___:
$i = 1;
while ( $i <= 10 ) {
$i = $i + 2;
}
echo "Loop Example 2: $i <br />";
Standard loop, where $i takes the values ___, ___, ___, ___, ___, ___, ___, ___:
$i = 1;
while ( $i <= 100 ) {
$i = $i * 2;
}
echo "Loop Example 3: $i <br />";
Introduction to Scripting, Databases and System Architecture, Spring 2007 The IT University of Copenhagen Page 2-20
Loop Exercises with while — loops2.php
Introduction to Scripting, Databases and System Architecture, Spring 2007 The IT University of Copenhagen Page 2-21
It is possible for PHP code to use data submitted by users in a form. Example:
<html><head><title>Exchange Bank</title></head>
<body> <h2>Exchange Bank</h2>
<form action="exchange.php">Enter value in kroner:
<p><input name="kroner" /></p>
<p><input type="submit" value="Get Dollar Amount" /></p>
</form>
</body></html>
<html><head><title>Exchange Bank</title></head>
<body> <h2>Exchange Bank</h2>
<? $kroner = $_REQUEST[’kroner’]; $rate = 8.43; $fee = 20.0;
$dollars = ($kroner - $fee) / $rate;
$dollars = number_format($dollars, 2, ",", ".");
echo "For DKr. $kroner you receive \$$dollars"; ?>
<p><a href="exchange.html">New Computation</a>
</body></html>
What problems does this exchange service have? Is the service robust?
Introduction to Scripting, Databases and System Architecture, Spring 2007 The IT University of Copenhagen Page 2-22
PHP scripts on the Web server at the IT University of Copenhagen
When a PHP script (a file with extension .php) is stored in a user’s folder
W:\f2007\DSDS\user \test.php
the script is executed when a client requests the file relative to the user ’s home page:
http://itu.dk/stud/f2007/DSDS/user /test.php
• Temperature Conversion
• Multiplication Service
• Apple Pie Service
Introduction to Scripting, Databases and System Architecture, Spring 2007 The IT University of Copenhagen Page 2-23
Next Lecture
Introduction to Scripting, Databases and System Architecture, Spring 2007 The IT University of Copenhagen Page 2-24