first lecture rh

62
php.infonomics.nl Introduction to Web programming with PHP 2012-2013 Course code EBS2040 Raoul Haenbeukers

Upload: kasieki

Post on 10-Nov-2015

216 views

Category:

Documents


0 download

DESCRIPTION

First

TRANSCRIPT

  • php.info

    nom

    ics.n

    l

    Introduction to Web programming with PHP

    2012-2013

    Course code EBS2040

    Raoul Haenbeukers

  • php.info

    nom

    ics.n

    l

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

    nom

    ics.n

    l

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

    nom

    ics.n

    l

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

    nom

    ics.n

    l

    The Book

    Larry Ullman

    PHP for the web, fourth edition

    Peachpit press

    ISBN 0-321-73345-2

    (Fourth edition is different from other

    editions)

    5

    Look at http://www.dmcinsights.com/phpvqs3/ for errata etc.

  • php.info

    nom

    ics.n

    l

    Examination 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.info

    nom

    ics.n

    l

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

    nom

    ics.n

    l

    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.

    8

  • php.info

    nom

    ics.n

    l

    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

    9

  • php.info

    nom

    ics.n

    l

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

    nom

    ics.n

    l

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

    nom

    ics.n

    l

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

    nom

    ics.n

    l

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

    nom

    ics.n

    l

    Current situation

    Basically PHP versus ASP.NET

    14

  • php.info

    nom

    ics.n

    l

    Sites running PHP

    Examples of sites running PHP:

    Wikipedia

    Facebook

    Wordpress

    and many others

    15

  • php.info

    nom

    ics.n

    l

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

    nom

    ics.n

    l

    17

    Pure html

    First simple example

    This is my first simple html example

    Example in a browser

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

  • php.info

    nom

    ics.n

    l

    18

    PHP-HTML

    First simple example

    Example in a browser

  • php.info

    nom

    ics.n

    l

    19

    PHP-HTML

    Second simple example

    Today, it is

    Example in a browser

  • php.info

    nom

    ics.n

    l

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

    Browser receives file, interprets

    it and displays the contents

    5

    20

    2

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

    Server looks for the

    file (in this case

    index.html) at a

    specified place

    3

  • php.info

    nom

    ics.n

    l

    1 User clicks on a link or types an address (URL), now a php file

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

    5

    21

    2

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

    PHP request

  • php.info

    nom

    ics.n

    l

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

    nom

    ics.n

    l

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

    nom

    ics.n

    l

    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:

    : start with html

    : stop using html

    24

  • php.info

    nom

    ics.n

    l

    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

    here comes the header information

    here comes the body of the page

    25

  • php.info

    nom

    ics.n

    l

    Header info, the formal one (using XHTML)

    Just make a copy and use it all the time!

    Put your title here!

    26

  • php.info

    nom

    ics.n

    l

    The paragraph-tag defines paragraphs and should be closed by the "end-paragraph" tag .

    A line break is presented by

    (Note the -tag does not have a closing counterpart and is in xhtml written as )

    The title is given in the tags.

    Headings are within heading-tags, from 1 to 6: etc.

    Example (also view source) 27

  • php.info

    nom

    ics.n

    l

    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

    28

  • php.info

    nom

    ics.n

    l

    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 and but there should be some attributes in between. The main attributes is href="" which refers to another page (or email address etc).

    Example:

    link to the infonomics php server

    29

  • php.info

    nom

    ics.n

    l

    Opening a new browser window is simply done by adding the target= "_blank" attribute to the anchor tag:

    link to the

    infonomics php server in a new

    window

    Examples in a browser

    30

  • php.info

    nom

    ics.n

    l

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

    nom

    ics.n

    l

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

    nom

    ics.n

    l

    Adding PHP to HTML (Ch 1)

    Escape from HTML into PHP can be done by including your code in the tag:

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

    nom

    ics.n

    l

    34

    Third simple example

  • php.info

    nom

    ics.n

    l

    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:

    In php:

    // single line comment

    /* multiple line

    comment */

    35

  • php.info

    nom

    ics.n

    l

    Output (Chapter 1)

    By using echo or print

    The output goes to the users browser (through an HTML file)

    Echo can have multiple arguments:

    36

    Example in a browser

  • php.info

    nom

    ics.n

    l

    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

  • php.info

    nom

    ics.n

    l

    Variables (Chapter 2) 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.info

    nom

    ics.n

    l

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

    nom

    ics.n

    l

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

    nom

    ics.n

    l

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

    nom

    ics.n

    l

    $a = 2; //an integer

    $a = 2.2; //a double

    $a = "two"; //a string

    $a = 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.info

    nom

    ics.n

    l

    Common mistake

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

    43

    ; Each statement ends with a ;

  • php.info

    nom

    ics.n

    l

    44

    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.

  • php.info

    nom

    ics.n

    l

    45

    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

  • php.info

    nom

    ics.n

    l

    46

    Forms in HTML

    Form tag :

    Input tag :

    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

  • php.info

    nom

    ics.n

    l

    47

    Forms in HTML

    Example

    (also have a look at the source code)

  • php.info

    nom

    ics.n

    l

    48

    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

  • php.info

    nom

    ics.n

    l

    49

    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)

  • php.info

    nom

    ics.n

    l

    50

    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

  • php.info

    nom

    ics.n

    l

    51

    We have a form with

    and an input field:

    In the file result.php we can retrieve the passed information by using:

    $fn = $_POST['firstname'];

    Note the underscore and POST in capital

  • php.info

    nom

    ics.n

    l

    52

    Variable Types

    Recall that a variable can be:

    Integer (whole numbers)

    Double (floating point)

    String (sequence of characters)

    Boolean

    Array

    Object (not treated here)

  • php.info

    nom

    ics.n

    l

    53

    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

  • php.info

    nom

    ics.n

    l

    54

    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)

  • php.info

    nom

    ics.n

    l

    55

    Strings

    Strings: a collection of characters enclosed with single or double quotes.

    PHP has many string functions to manipulate strings

  • php.info

    nom

    ics.n

    l

    56

    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)*/

  • php.info

    nom

    ics.n

    l

    57

    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

  • php.info

    nom

    ics.n

    l

    58

    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

  • php.info

    nom

    ics.n

    l

    59

    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.

  • php.info

    nom

    ics.n

    l

    60

    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

  • php.info

    nom

    ics.n

    l

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

    nom

    ics.n

    l

    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