Sunteți pe pagina 1din 62

php.infonomics.

nl

Introduction to Web
programming with PHP
2012-2013

Course code EBS2040


Raoul Haenbeukers

php.infonomics.nl

Whats this course about


Introduction to computer
programming in general
(what is a computer language, what
is PHP etc).
Practice with PHP
Practice with databases
Practice with dynamic web sites
2

php.infonomics.nl

Todays lecture
What is this course about
Structure of the course
Examination
The website
Introduction to PHP
Brief introduction to HTML
Overview of Chapters 1 - 5
3

php.infonomics.nl

Structure
First week well go through the
book in 2 lectures
Study the covered material
Make all exercises
Practice in the Library (or at home
any other place)
Second week: work on a project
4

php.infonomics.nl

The Book
Larry Ullman
PHP for the web, fourth
edition
Peachpit press
ISBN 0-321-73345-2
(Fourth edition is different from other
editions)
Look at http://www.dmcinsights.com/phpvqs3/ for errata etc.
5

Examination
php.infonomics.nl

Two parts

Making all exercises, not graded


Making one project, graded (four difficulty levels)

All exercises and projects can be found on the website


for this course:
php.infonomics.nl
All exercises and projects should be uploaded to that
server
Grades depend on the difficulty of the project (starting
grades are 6, 7, and 8 for working projects according to
the description), the bonus points obtained (+1 pt.
max) and the way it is coded (in normal circumstances
+ or 1 pt.).
6

php.infonomics.nl

Examination
Deadlines:
Monday, January 20th, upload all exercises before
17.00 hours
Before Monday, January 27th, upload your project
before midnight, so Sunday 23.59 at the latest

It should be your own work!


All the exercises and the project is individual work or
the result of group work in small groups, max 2
persons. (If project is the work of 2 persons, 1 point
is subtracted from grade)
No unnoticed copying of code is allowed (plagiarism)
small parts of others may be used but this should be
mentioned in the code
7

php.infonomics.nl

Examination
Use our server to upload and test
Please use our server to test your code
Do not use a separate server to test your
code
In case of doubt we keep the right to
organize a meeting to discuss the code and
you should be able to explain what you
have done.

php.infonomics.nl

php.infonomics.nl
You will today receive an email with a username
and password (password cannot be changed).

Needed to visit secured parts of the website,


such as the forum
Needed to access the database

php.infonomics.nl

The forum
See http://php.infonomics.nl, click on
"message board", login with your private
username and password (Note: do not use the
faculty user-id and password)
The forum can be used to post questions and
to give answers on material that is relevant for
this course.
You can copy/paste code into the forum, no
problem
This is the preferred channel of
communication
10

php.infonomics.nl

What will we do in this week?


Make the exercises
Write all code yourself, that is the only
way to learn programming
Keep on track with the book chapters
(next week, there is no time to read the
book)
Be active on the forum, also by
providing answers to questions posted
by fellow students
11

php.infonomics.nl

Next week
Work on the project in the reserved
hours in the computer room (under
supervision of staff members)
In the library
At home

Use the forum to ask/answer questions
12

php.infonomics.nl

What is PHP
PHP is a recursive acronym and stands
nowadays for "PHP: Hypertext
Preprocessor", but originally it was called
"Personal Home Page Tools". PHP is
created by Rasmus Lerdorf.
PHP is attached (or embedded) to HTML
such that you can create dynamic
websites, that is, the content is created at
the moment you retrieve that webpage
13

php.infonomics.nl

Current situation
Basically PHP versus ASP.NET

14

php.infonomics.nl

Sites running PHP


Examples of sites running PHP:
Wikipedia
Facebook
Wordpress
and many others

15

php.infonomics.nl

A Simple example
HTML (Hypertext Markup Language)
uses tags to define the structure of
a page but all pages are already
defined on the server and the user
can only see these pages, follow
hyperlinks etc. In pure HTML, there
is no way to change the content of
pages on the fly.
16

php.infonomics.nl

Pure html
<!-- This file is created by Huub Meijers-->
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0
Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="content-type" content="text/html;
charset=iso-8859-1" />
<title>First simple example</title>
</head>
<!--First Simple example in pure HTML by Huub Meijers-->
<body>
This is my first simple html example
</body>
</html>

Example in a browser
(note that for the lectures I use a larger font size which is not in17
the HTML code above)

php.infonomics.nl

PHP-HTML
<!--First Simple example in PHP-HTML by Huub Meijers-->
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0
Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="content-type" content="text/html;
charset=iso-8859-1" />
<title>First simple example</title>
</head>
<body>
<?php print "This is my first simpel php-html example"; ?>
</body>
</html>

Example in a browser
18

php.infonomics.nl

PHP-HTML
<!--Second Simple example by Huub Meijers-->
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0
Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="content-type" content="text/html;
charset=iso-8859-1" />
<title>Second simple example</title>
</head>
<body>
Today, it is <?php print Date("l F d, Y");?>
</body>
</html>

Example in a browser
19

Normal HTML request

php.infonomics.nl

User clicks on a html link or types an address (URL)

3
Server looks for the
file (in this case
index.html) at a
specified place

2
GET http://www.infonomics.nl/index.html

4
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0
Transitional//EN"

Browser receives file, interprets


it and displays the contents

20

PHP request
1

User clicks on a link or types an address (URL),


now a php file

php.infonomics.nl

3
2

Server looks for the file (in


this case index.php),
processes it through the
scripting engine, accesses the
database if needed and sends
the result as HTML to the
browser

GET http://www.infonomics.nl/index.php

4
<!DOCTYPE html PUBLIC
"-//W3C//DTD XHTML 1.0
Transitional//EN"

Browser receives file and


displays the contents as HTML;
The user cannot see that the
file was created by PHP

Scripting
engine

File system
Databases
Etc.

21

php.infonomics.nl

Use .php instead of .htm


If a web page includes a php script, you should always
give it the extension ".php" since otherwise, the script is
not passed through the scripting engine but directly to
the Webserver. For instance, if we call the second
example:
SecondSimpleExample.htm
than the resulting page would look like:
Second Example in Browser, named *.htm
22

php.infonomics.nl

index.htm(l) or index.php
In most cases youll not specify a filename if you
enter a web page or URL in the address bar.
For instance you specify php.infonomics.nl and
not php.infonomics.nl/index.html
This is due to the default behaviour of the web
server. If you dont specify a filename, the file
index.html (or index.htm, or index.php) is
loaded automatically.
So index.htm(l) or index.php is treated as the
default filename and this file is always used as
the main entry point of your web site.
23

php.infonomics.nl

Short Introduction to HTML


the HyperText Markup Language (HTML)
is created to format web-pages so they
can be viewed by a browser.
The html language makes use of tags to
structure the pages
a tag starts with a < and ends with a >
The most basic tag is the HTML-tag:
<html>
: start with html
</html> : stop using html
24

php.infonomics.nl

The next structure is the head and the


body
The head contains header information,
like the title of the page, the body
contains the content itself
<html>
<head>
here comes the header information
</head>
<body>
here comes the body of the page
</body>
</html>
25

php.infonomics.nl

Header info, the formal one (using


XHTML)
Just make a copy and use it all the time!
<!-- This file is created by ###put your name here!!###
-->
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0
Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="content-type" content="text/html;
charset=iso-8859-1" />
<title>Put your title here!</title>
</head>
26

php.infonomics.nl

The paragraph-tag <p> defines


paragraphs and should be closed by the
"end-paragraph" tag </p>.
A line break is presented by <br>
(Note the <br>-tag does not have a
closing counterpart and is in xhtml
written as <br />)
The title is given in the <title></title>
tags.
Headings are within heading-tags, from
1 to 6: <h1></h1> etc.
Example (also view source)

27

php.infonomics.nl

It is very important to add


comments to your pages since the
enhance readability of the code.
Moreover, during this course you
should provide your name in the
first line of all html and php
pages.
In HTML, comment starts with
<!-- and ends with -->

28

php.infonomics.nl

One of the main features of web pages


is that they can link to others. This is
done by adding anchors. The general
format is <a> and </a> but there
should be some attributes in between.
The main attributes is href="" which
refers to another page (or email
address etc).
Example:
<a
href="http://php.infonomics.nl" >
link to the infonomics php server
</a>
29

php.infonomics.nl

Opening a new browser window is simply


done by adding the target= "_blank"
attribute to the anchor tag:
<a href="http://php.infonomics.nl"
target="_blank" > link to the
infonomics php server in a new
window</a>

Examples in a browser

30

php.infonomics.nl

Finally, when using php you often want to get


some input from users. This can be done by the
FORM-tag. Examples are given in the
"Introduction to HTML" page on the
php.infonomics.nl web site, menu option:
Course Material
Go

Also tables are often used to present data.


Please also study the way how to create tables.

31

php.infonomics.nl

To Do
If you are not familiar with HTML, read
the "Introduction to HTML" section today
and practice on the server or your own
computer.
How?
Create a new file, edit it with e.g.
notepad, syn text editor, notepad++ or
any other tool, rename it to name.htm, if
you double-click on that file, the browser
will start and youll see the result. (If you
dont use php, you dont have to upload
the page to the server)
32

php.infonomics.nl

Adding PHP to HTML (Ch 1)


Escape from HTML into PHP can be done
by including your code in the tag:
<?php
?>
Everything in the tag is treated as PHP, at
least if the file extension is .php
You can also switch between php and
pure html in a single page by using
more php-tags.
Example:
33

php.infonomics.nl

<!-- This file is created by Huub Meijers-->


<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="content-type" content="text/html;
charset=iso-8859-1" />
<title>Third simple example</title>
<!--Third Simple example in PHP-HTML by Huub Meijers-->
</head>
<body>
<font size=+3>
<?php $name=Anton"; ?>
Today, it is <?php print Date("l F d, Y");?>
<br />
How are you <?php print $name;?>?
</font>
</body>
</html>
Example in a browser (also view source and see that the PHP
code is not included, only the result. Note that I added the
font size to make the text more readable during the lectures)
34

php.infonomics.nl

Adding comments
It is very important that you add comments to your source
code. First of all we require that you write your name on
the first line.
But if scripts become more complicated, you need to add
comments to explain what you do at that stage. This in
order to make it easier to change the code afterwards.
Comments in html:
<!-- here is a comment -->
In php:
// single line comment
/* multiple line
comment */
35

php.infonomics.nl

Output (Chapter 1)
By using echo or print
The output goes to the users browser
(through an HTML file)
Echo can have multiple arguments:
<?php
print "Print this and that and so <br />";
print ("Print this and that and so <br />");
echo "print this", " and that", " and so";
//is also a valid statement
?>

Example in a browser
36

php.infonomics.nl

Output
Note that special characters need a special
treatment.
For instance if you want to print a
double quote ("), this can be cumbersome.
In that case, use an escape by adding a
backslash to the character
E.g.:
print "this is a \"quote\" from a text";

The escape actually means that the next


character should be interpreted as a normal
character without a special function
37

Variables (Chapter 2)
php.infonomics.nl

A variable is a container that holds data


Variables always starts with a $-sign, followed by the name
itself.
The name must always start with a character (A..Z, a..z)
After the first character, you may use numbers and the
underscore (e.g. $name1, $first_name etc.)
Variable names are case sensitive so $FirstName is different
from $firstname
Always use logical names
Use a consistent style, common is:

Camel style, e.g. $InterestRate, $DayOfTheWeek


Underscores, e.g. $interest_rate, $day_of_the_week
38

php.infonomics.nl

Variables: Examples
$name = Anton";
/* assigns the data (in this case a string,
i.e. a piece of text) to the variable called
$name */
$amount = 2.2;
/* assign the value 2.2 to the variable
called $amount */

39

php.infonomics.nl

Variables: Summary
So
All variables start with a $-sign
The value is based on the most recent
assignment
Assignment through the
"=" operator
Each statement ends with a ;
$my_num = "this is a string";
$my_num = 5; //this is an integer
print "$my_num"; //this prints a 5
40

php.infonomics.nl

Types in PHP

Integer (whole numbers)


Double (floating point number)
String (sequence of characters)
Boolean (have value TRUE or FALSE
(Chapter 6)
Array (collection of other types)
(Chapter 7)
Objects (not treated in the book or
course)
But PHP is very flexible!
41

php.infonomics.nl

$a
$a
$a
$a

=
=
=
=

2; //an integer
2.2; //a double
"two"; //a string
FALSE; //a Boolean

Can be used after each other, of course


the last assignment will be the one that
is stored in the variable $a

Each of the above lines is called a


statement and a statement is closed
with a semicolon ;
42

php.infonomics.nl

Common mistake
90% of all mistakes are caused by
just one character, the:

Each statement ends with a ;


43

php.infonomics.nl

Forms
Forms are used to pass information
from the user to the program
The program can pass this information
to a database, or use this information
directly to e.g. calculate something etc.
We can have two files: one with the
form and one that retrieves and handles
the information.

44

php.infonomics.nl

Forms
HTTP is stateless, no information is
passed between HTTP requests so also
not between several html or php pages
PHP forms do allow for passing
information between pages
Get and Post, the basic methods to pass
information between pages
45

php.infonomics.nl

Forms in HTML
Form tag : <form> </form>
Input tag : <input> </input>
Attributes:
Form:
action= "filename.php", method = get or post
Input:
type=text, textarea, submit, reset, hidden
name : name of the input field
value : default value
46

php.infonomics.nl

Forms in HTML

Example
(also have a look at the source code)

47

php.infonomics.nl

Two methods: GET and POST


The GET-method passes information
between pages by adding the
variable names and values through
the URL, e.g.
mysite.org/page.php?name=Anton
View the code of the form
View the code of the php-program
View the result in browser
48

php.infonomics.nl

Post is preferred
The data are posted in the body of
the form
Variables are not visible
Larger amount of data
Cannot be book-marked
(Some firewalls block these data,
but this has become rare)
49

php.infonomics.nl

Post, an example
An example with radio-buttons.
(Radio buttons allow just one item to be
checked, the others are un-checked
automatically)
View the code of the form
View the code of the php-program
View the result in browser
50

php.infonomics.nl

We have a form with


<form action="result.php"
method="post"> and an input field:
<input type="text" name="firstname">
In the file result.php we can retrieve the
passed information by using:
$fn = $_POST['firstname'];
Note the underscore and POST in capital

51

php.infonomics.nl

Variable Types
Recall that a variable can be:

Integer (whole numbers)


Double (floating point)
String (sequence of characters)
Boolean
Array
Object (not treated here)

52

php.infonomics.nl

Math functions by example


Pow(2,3) gives 8 (2^3)
Floor(2.5) gives 2
Ceil(2.5) gives 3
Round(2.5) gives 3
Abs(-2) gives 2
Min(2,3,4) gives 2
Max(2,3,4) fives 4
Exp(2) e to the power 2
53

php.infonomics.nl

Using ++
In PHP (like C++) you are allowed to
increment a variable with 1 by using ++. So:
$x = 1;
//assign the value 1 to $x
$x++;
//increment $x by 1
print ("$x"); //print $x, so a 2
This is similar to:
$x = 1;
//assign the value 1 to $x
$x = $x + 1; //new x is old x plus 1
Print ("$x"); //print $x, so a 2
The same holds true for -- (decrement by 1)
54

php.infonomics.nl

Strings
Strings: a collection of characters
enclosed with single or double
quotes.
PHP has many string functions to
manipulate strings

55

php.infonomics.nl

Concatenation
Adding two strings together is
simple done by a dot-operator
$firstname=Anton";
$lastname =Corbijn";
$name=$firstname . " " . $lastname;
print("$name");
/*will print Anton Corbijn (note the
space we have added)*/
56

php.infonomics.nl

Many useful string functions


Strpos(string, stringtofind) gives the position
of the first instance of stringtofind in string,
starting at 0
E.g. strpos("Rasmus","u") returns a 4
Strlen gives the length of the string
Substr(string,pos) returns the substring of
string, starting at position pos (actually
pos+1)
E.g. substr("example",5) returns "le"
And many other string functions (see chap 5)
or even more at:
http://www.php.net/manual/en/ref.strings.php

57

php.infonomics.nl

Addslashes and Stripslashes


Two functions are very useful if we
want to store data in a database.
As we will see Wednesday, adding
a single quote in a text to a
database is problematic, for this
reason we use these two functions.
Addslashes adds slashes to all kinds
of special characters and
stripslashes removes them
58

php.infonomics.nl

Example
Suppose you want to store the name: Jeanne
dArc in a database or the text: he said: PHP is
very powerful. In both cases PHP has some
troubles, for instance look at:
$text = "He said: "PHP is very powerful. "";
This is impossible since PHP assumes that the
string finishes after the second ".
The solution is to add slashes:
$text = "He said: \"PHP is very powerful.\""; But
if the data come from the input of a user, e.g. by
using a form, we dont know in advance where
to put the slashes. The function addslashes does
this for us.
59

php.infonomics.nl

Addslashes
So if you want to store text in a database and this text
comes from a form (by using POST) we can have:
$inputtext=$_POST['text'];
$text=addslashes($inputtext);

Which is equal to (note the double use of the variable


$text):
$text=$_POST['text'];
$text=addslashes($text);
And this also can be done in one line:
$text = addslashes($_POST['text']);
//now save $text in the database

60

php.infonomics.nl

Final remarks
Be active on the Forum
Make all exercises (required!!)
Try to understand the book by
typing in the examples, changing
them a bit etc.

Programming is not just reading but


also doing
61

php.infonomics.nl

Try it: today


As part of the exercises today you
are asked to create some simple
files and to upload them to the
server.
Try it and do not hesitate to ask
questions on the forum if things
are not working
62

S-ar putea să vă placă și