Sie sind auf Seite 1von 7

Web Programming with PHP

I Introduction to Web programming


Web Publishing with Databases, F2006 I The programming language PHP — the first script
Lecture 2 I Embedding of code and comments
I Variables and computation with numbers
I Something about character strings
IT University of Copenhagen I Conditional statements — if-statements

Monday February 6, 2006


I Loops — while-loops
I Use of form variables
I PHP scripts on the Web server

Introduction to Web Programming Web Programming with PHP


PHP is a programming language for Web programming
INTERNET Example — php/hello.php:
WEB SERVER HTTP
DATABASE SQL BROWSER
(MySQL) (Apache) <html><head><title>Hello World</title></head>
<body>
HTTP
<?php
PHP ... HTML BROWSER echo "<p>Hi There, </p>";
FILE FILE echo "<p>Greetings from a simple PHP script</p>"; ?>
</body>
</html>
I Client (browser) requests a page on a Web server
I Web server executes the program I PHP code is embedded in HTML code with the use of
I The Web server program accesses (for instance) a <?php and ?>
database on the Web server machine I The PHP command echo sends its argument (a character
I The result of the execution (HTML code) is sent to the string "...") to the browser
client I PHP commands are ended with the character ‘;’
What Is Sent To The Browser Code Embeddings and Comments
I The following example illustrates three ways of
embedding code in a PHP document — php/embed.php:
<html><head><title>Embeddings</title></head>
<body> <h2>Tree forms for embedding PHP in HTML</h2>
I When a browser requests the page hello.php on the <ol>
Web server, the PHP code is executed, which results in <? echo "<li>The simple form</li>"; ?>
HTML code, which again is sent to the browser: <?php echo "<li>A slightly longer form</li>"; ?>
<html><head><title>Hello World</title></head>
<script language="PHP">
<body> // Comments in PHP code is not sent to the browser
<p>Hi There, </p><p>Greetings from a simple PHP script</p> echo "<li>The somewhat longer form</li>";
</body> </script>
</html> </ol>
</body>
</html>
I Usually, we shall use the slightly longer form
<?php ... ?>
I Comments that extend over several lines can be written
/* ... */

Variables in PHP Example: Homepage

I Example — php/homepage.php:
<html><head><title>Home page</title></head>
<body>
I Variables are used for storing values during the execution <h2>
of a PHP script <?php $name = "Ken Friis Larsen";
echo "Home page for ";
I Values can, for instance, be character strings and echo $name;
numbers ?>
I Names of variables are chosen freely by the programmer, </h2>
This page is maintained by
although variable names must start with the character $
<?php echo $name ?>
</body>
</html>
I Notice that a variable can be referred to more than once
after it has been initialized with a value.
Computation with Numbers Example: Dollar Exchange
Example — php/dollars.php:

<html><head><title>Dollars</title></head>
<body>
<h2>Dollars</h2>
<?php
There are two types of numbers in PHP:
$rate = 6.13;
1. Integers: 3, 9, 0, -1, ... $kroner = 1000.0;
2. Doubles (double precision): 3.0, 9.2, 0.13, -23.2, ... $dollars = ($kroner - 20.0) / $rate;
echo "For DKr. $kroner you receive \$$dollars"; ?>
The usual number operations (e.g., +,-,*, and /) can be used
</body>
in computations. </html>

Notice:
I It is possible to refer to a variable inside a character
string "..."
I To insert a dollar-sign ($) in a character string, the
character ‘\’ should be placed before the dollar-sign

Arithmetic Operations and Evaluation Order Pop Quiz


(precedence)
Operator Meaning Examples
Multiplication 1.5 * 60 24 * 60 What is the type and value of the following expressions?
*
/ Division 134.0 / 60 134 / 60
% Remainder — 134 % 60 Arithmetic Expression Result Type Result Value
+ Addition 1.1 + 60 14 + 60 1.5 * 60 double 90
- Subtraction 1.1 - 60 134 - 60
150.0 / 60 double 2.5
150 / 60 double 2.5
I Operators with a high precedence bind stronger than
134 % 60 integer 14
operators with a low precedence, and are therefore
1.1 + 60 double 61.1
evaluated first.
1.1 - 60 double -58.9
I The operators *, /, and % have higher precedence than +
and -, thus operations with these operators are evaluated
first.
I When operators have the same precedence (same
degree of binding), evaluation goes from left to right.
More On Evaluation Order More About Character Strings
I Character strings in PHP can be expressed either by
I It is possible to construct arbitrarily complicated "..." or by ’...’.
expressions, for instance: I In character strings on the form ’...’, variables cannot
22 - 34 + 43 % 34 * 23 + 122 / 43.22 * 23 + 43 be referred to.
evaluates to 302.924 I Example: If the variable $name contains the value Per,
I Without precedence rules, expressions can be the two statements
ambiguous: Does 2+4*4 evaluate to 24 or 18? echo "Your name is $name";
I Because * has a higher precedence than +, 4*4 is echo ’Your name is $name’;
evaluated first whereafter 2 is added. result in
I To evaluate the addition first, parentheses can be used: Your name is Per
The expression (2+4)*4 evaluates to 24. Your name is $name
I Because * and / have the same precedence, 2*5/2 is I Other special characters in character strings on the form
evaluated from left to right, resulting in the value 5. "...":
I \\ : backslash
I To evaluate the division first, parentheses can be used: I \n : newline
The expression 2*(5/2) evaluates to the value 5. I \t : tabulator
I \" : double quote

Appending of Strings Conditional Statements in PHP


I If-statements are used for executing different code
depending on some condition
I Example — php/account.php:
I Strings can be appended in PHP by using the character ‘.’
<html><head><title>Account</title></head>
I Example — php/strings.php: <body>
<html><head><title>Character Strings</title></head> <?php $dollars = 8;
<body> if ( $dollars == 1 ) {
<?php $firstname = "Ken Friis"; echo "You have 1 Dollar on you account";
$lastname = "Larsen"; } else {
echo "You have $dollars Dollars on your account";
$name = $firstname . " " . $lastname;
}
echo "My name is $name";
?>
?>
</body>
</body> </html>
</html>
I A condition is either FALSE (the value 0) or TRUE
(different from 0).
I If $dollars == 1 is FALSE (value 0), the else-branch is
executed. Otherwise the if-branch is executed.
The If-statement in PHP Example: Body Mass Index — php/bmi.php
I The general format:
<html><head><title>Body Mass Index</title></head>
if ( condition1 ) {
<body>
statement1
} elseif ( condition2 ) { <h2>Body Mass Index</h2>
statement2 <?php
} else { $weight = 70.0; $height = 183.0;
statement3 echo "Weight: $weight kg. <br />Height: $height cm.<br />
} $bmi = $weight / (($height/100.0) * ($height/100.0));
echo "Your BMI is $bmi<br />";
I Meaning: if ( $bmi < 20.0 ) {
1. compute the value of condition1. echo "Your BMI is too low.";
2. if different from 0 (i.e., TRUE) then execute statement1, } elseif ( $bmi < 25.0 ) {
otherwise echo "Your BMI is normal.";
3. compute the value of condition2 } else {
4. if different from 0 (i.e., TRUE) then execute statement2, echo "Your BMI is too high.";
otherwise }
5. execute statement3 ?>
I An arbitrary number of elseif-branches can be used. </body>
</html>
I It is possible to omit the closing else-branch.

Comparison Operators and their Precedence Pop Quiz No. 2

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


Operator Meaning Example
< Less than $x < 60
Logical Expression Result Value
<= Less than or equal to $x <= 60
> Greater than $x > 60 1 == 1 1
>= Greater than or equal to $x >= 60 $x != $y 1
== Equal to $x == 60 $x < 3 + $y 1
!= Different from $x != 60 ($x + $y > 3) == 0 Bad example
0 != $x < 3 Bad example
$x == $y == 0 Error
I The arithmetic operators *, /, %, +, - bind stronger than
the comparison operators <, <=, >, >= Remember: The number 1 denotes TRUE and 0 denotes
I The comparison operators <, <=, >, >= bind stronger than FALSE.
== and !=
Logical Operators and their Precedence Loops in PHP
How can we output the text "I love Web programming" 20
Operator Meaning Example times?
! Not (Negation) !($x == 60)
Bad solution
&& And (Conjunction) 0 <= $x && $x < 60
|| Or (Disjunction) $x < 0 || $x >= 60 echo "I love Web programming<br />";
...18 times...
I The operator ! binds stronger than &&, which binds echo "I love Web programming<br />";
stronger than ||.
I ! also binds stronger than the comparison operators and
Better solution
the arithmetic operators.
use a while-loop for repetition — php/love.php:
I && and || bind weaker than the comparison operators
and the arithmetic operators. $counter = 20;
I Evaluation goes from left to right. while ( $counter >= 1 ) {
I If exp1 is FALSE in exp1 && exp2 then exp2 is not echo "I love Web programming<br />";
evaluated. $counter = $counter - 1;
I If exp1 is TRUE in exp1 || exp2 then exp2 is not }

evaluated.

Syntax for while-loops Examples of while-loops — php/loops1.php


I Standard loop, $i takes the values 10 , 9 , 8 , 7 , 6 , 5 , 4 ,
I General format: 3,2,1,0:
while ( condition ) { $i = 10;
statement while ( $i >= 1 ) {
} $i = $i - 1;
}
I Meaning:
echo "Loop Example 1: $i <br />";
1. Evaluate the condition I Standard loop, $i takes the values 1 , 3 , 5 , 7 , 9 , 11 :
2. If the result is different from 0 (i.e., TRUE), then evaluate
$i = 1;
statement, and continue at 1. while ( $i <= 10 ) {
I Thus, the body of the while-loop (statement) is $i = $i + 2;
}
evaluated as long as the condition is TRUE
echo "Loop Example 2: $i <br />";
I Often used while-construction
I Standard loop, $i takes the values 1 , 2 , 4 , 8 , 16 , 32 ,
initialization; 64 , 128 :
while ( condition ) { $i = 1;
statement while ( $i <= 100 ) {
increment; $i = $i * 2;
} }
echo "Loop Example 3: $i <br />";
Loop Exercises with while — php/loops2.php Use of Form Variables
I Write a while-loop that outputs 64, 32, 16, 8, 4, 2, 1:
The File exchange.html: The File exchange.php:
$i = 64 ;
while ( $i >= 1 ) { <html><head> <html><head>
echo "$i, "; <title>Exchange Bank</title> <title>Exchange Bank</title>
$i = $i / 2 ; </head> </head>
} <body> <h2>Exchange Bank</h2> <body> <h2>Exchange Bank</h2>
I Write a while-loop that outputs 2, 4, 6, 8, . . . , 100: <form action="exchange.php"> <?php
$i = 2 ; Enter value in kroner: $rate = 8.43; $fee = 20.0;
while ( $i <= 100 ) { <p><input name="kroner" /> $kroner = $_GET[’kroner’];
echo "$i, "; </p> $dollars = ($kroner - $fee) / $rate;
$i = $i + 2 ; <p><input type="submit" $dollars = number_format($dollars, 2,
} value="Get Dollar Amount" ",", ".");
/> echo "For DKr. $kroner you receive
I Write a while-loop that outputs 100, 110, 120, . . . , 200: </p> $$dollars"; ?>
$i = 100 ; </form> <p><a href="exchange.html">
while ( $i <= 200 ) { </body></html> New Computation</a>
echo "$i, "; </body>
$i = $i + 10 ; </html>
}

PHP scripts on the Web server at the ITU Next Lecture

When a PHP script (a file with extension .php) is stored in a


user’s folder
H:\public_html\test.php We continue with PHP:

the script is executed when a client requests the file relative I Technologies for Web sites that are programs
to the user’s home page: I for-loops
http://www.itu.dk/people/user/test.php I Built-in PHP functions
I User defined functions
Exercises — Problem Set 2
I Reuse of code
I Temperature Conversion
I Multiplication Service
I Apple Pie Service

Das könnte Ihnen auch gefallen