Documente Academic
Documente Profesional
Documente Cultură
nl
Introduction to Web
programming with PHP
2012-2013
php.infonomics.nl
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
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
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).
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
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
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
php.infonomics.nl
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"
20
PHP request
1
php.infonomics.nl
3
2
GET http://www.infonomics.nl/index.php
4
<!DOCTYPE html PUBLIC
"-//W3C//DTD XHTML 1.0
Transitional//EN"
Scripting
engine
File system
Databases
Etc.
21
php.infonomics.nl
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
php.infonomics.nl
php.infonomics.nl
php.infonomics.nl
27
php.infonomics.nl
28
php.infonomics.nl
php.infonomics.nl
Examples in a browser
30
php.infonomics.nl
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
php.infonomics.nl
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";
Variables (Chapter 2)
php.infonomics.nl
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
php.infonomics.nl
$a
$a
$a
$a
=
=
=
=
2; //an integer
2.2; //a double
"two"; //a string
FALSE; //a Boolean
php.infonomics.nl
Common mistake
90% of all mistakes are caused by
just one character, the:
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
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
51
php.infonomics.nl
Variable Types
Recall that a variable can be:
52
php.infonomics.nl
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
57
php.infonomics.nl
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);
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.
php.infonomics.nl