Sei sulla pagina 1di 12

Introduction to Scripting, Databases and System Architecture, Spring 2007

Lecture 2, Monday February 5, 2007

Web Programming with PHP

• Introduction to Web programming


• The programming language PHP — the first script
• Embedding of code and comments
• Variables and computation with numbers
• Something about character strings
• Conditional statements — if-statements
• Loops — while-loops
• Use of form variables
• PHP scripts on the Web server

Introduction to Scripting, Databases and System Architecture, Spring 2007 The IT University of Copenhagen Page 2-1

Introduction to Web Programming

Overview:
INTERNET
WEB SERVER HTTP
DATABASE SQL BROWSER
(MySQL) (Apache)

HTTP

PHP ... HTML BROWSER


FILE FILE

• Client (browser) requests a page on a Web server


• Web server executes the program
• 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

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

Introduction to Scripting, Databases and System Architecture, Spring 2007 The IT University of Copenhagen Page 2-2
Web Programming with PHP

PHP is a programming language for Web programming


Example — hello.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

Code Embeddings and Comments

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

<?php echo "<li>A slightly longer 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>

Usually, we shall use the simple form <? ... ?>


Comments that extend over several lines can be written /* ... */

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

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:

<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

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:

<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:

• 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

Introduction to Scripting, Databases and System Architecture, Spring 2007 The IT University of Copenhagen Page 2-6
Arithmetic Operations and Evaluation Order (precedence)

Operator Meaning Examples

* 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

Example: What is the type and value of the following expressions?

Arithmetic Expression Result Type Result Value

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

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

Introduction to Scripting, Databases and System Architecture, Spring 2007 The IT University of Copenhagen Page 2-9

Appending Character Strings

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

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>

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 like == :

< less than > larger than != different from


<= less than or equal >= larger than or equal

Introduction to Scripting, Databases and System Architecture, Spring 2007 The IT University of Copenhagen Page 2-11

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.

Introduction to Scripting, Databases and System Architecture, Spring 2007 The IT University of Copenhagen Page 2-12
Example: Body Mass Index — bmi.php

<html><head><title>Body Mass Index</title></head>


<body>
<h2>Body Mass Index</h2>
<? $weight = 70.0; $height = 180.0;
echo "Weight: $weight kg. <br />Height: $height cm.<br />";
$bmi = $weight / (($height/100.0) * ($height/100.0));
echo "Your BMI is $bmi<br />";
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.";
}
?>
</body>
</html>

Introduction to Scripting, Databases and System Architecture, Spring 2007 The IT University of Copenhagen Page 2-13

Comparison Operators and their Precedence

Operator Meaning Example

< Less than $x < 60


<= Less than or equal to $x <= 60
> Greater than $x > 60
>= Greater than or equal to $x >= 60
== Equal to $x == 60
!= Different from $x != 60

• 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

Let x have the value 2 and y have the value 4.

Logical Expression Result Value

1 == 1
$x != $y
$x < 3 + $y
($x + $y > 3) == 0
0 != $x < 3
$x == $y == 0

Remember: The number 1 denotes TRUE and 0 denotes FALSE.

Introduction to Scripting, Databases and System Architecture, Spring 2007 The IT University of Copenhagen Page 2-15

Logical Operators and their Precedence

Operator Meaning Example

! Not (Negation) !($x == 60)


&& And (Conjunction) 0 <= $x && $x < 60
|| Or (Disjunction) $x < 0 || $x >= 60

• 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.

Boolean Expression Result Value

! 0
! 1
! 1 == 0
! (1 == 0)
1 && 0
0 || 1
$x + $y > 3 && $x < $y
$x + $y == 3 || $x < 4

Remember: the value 1 denotes TRUE and 0 denotes FALSE.

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:

echo "I love Web programming<br />";


...18 times...
echo "I love Web programming<br />";

Better solution: use a while-loop for repetition — love.php:

$counter = 20;
while ( $counter >= 1 ) {
echo "I love Web programming<br />";
$counter = $counter - 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.

• What happens if the variable $counter is not decreased?

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:

(1) Evaluate the condition

(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

Often used while-construction — love.php:

initialization ;
while ( condition ) {
statement ;
increment ;
}

Introduction to Scripting, Databases and System Architecture, Spring 2007 The IT University of Copenhagen Page 2-19

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

What is the value of $i after each loop?

Introduction to Scripting, Databases and System Architecture, Spring 2007 The IT University of Copenhagen Page 2-20
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 + __ ;
}

Introduction to Scripting, Databases and System Architecture, Spring 2007 The IT University of Copenhagen Page 2-21

Use of Form Variables

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

The File exchange.html:

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

The File exchange.php:

<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

Exercises — Problem Set 2

• 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

We continue with PHP:

• Technologies for Web sites that are programs


• for-loops
• Built-in PHP functions
• User defined functions
• Reuse of code

Introduction to Scripting, Databases and System Architecture, Spring 2007 The IT University of Copenhagen Page 2-24

Potrebbero piacerti anche