Title: LIS651 lecture 0 Introduction to PHP with primitive data types
1LIS651 lecture 0Introduction to PHP with
primitive data types
- Thomas Krichel
- 2009-03-08
2today
- Introduction to the course
- Introduction to PHP
- Using form data in PHP
3course resources
- Course home page is at http//wotan.liu.edu/home/k
richel/courses/lis651p08a - The course resource page http//wotan.liu.edu/home
/krichel/courses/lis651 - The class mailing list https//lists.liu.edu/mailm
an/listinfo/cwp-lis651-krichel - Me.
- Send me email. Unless you request privacy, I
answer to the class mailing list. - Skype me at thomaskrichel. Get skype from
skype.com.
4today
- We introduce PHP. Understanding PHP is the most
difficult aspect of the course. - We look at how PHP can be used to show the data
that we get from the form. - You should think about what data to get and how
to show it. - Everybody will build an example form and then a
PHP script to show it. - Finally we build a new PHP script that contains
the form. So instead of two files, we only have
one.
5PHP introduction
- PHP is the PHP Hypertext Processor.
- It is a tool that allows for server-side
scripting. - Its predecessor is PHP/FI, Personal Home Page /
Forms Interpreter. - PHP/FI was released by Rasmus Lerdorf in 1995. It
was written in Perl. - PHP/FI version 2 was released in 1997. It was
written in C. - PHP version 5 is the current version.
6Apache and PHP
- When a file ends in .php, is not simply sent down
the http connection like other files. - Instead, apache sends the file to the PHP
processor. - It sends to the client whatever the PHP processor
returns. - The PHP processor is a module that lives inside
Apache.
7PHP language
- PHP is an interpreted language.
- You write a series of statements.
- Apache hands these statements to the PHP
interpreter. - The interpreter executes these statements one by
one. - When it find an error, it stops running and
signals the error. - Compiled languages are different. They read the
whole program before starting to execute it.
8try it out
- Remember we duplicate validated.html when
creating a new new file. - Right-click on validated.html, choose duplicate.
- You may be asked to supply your password again.
- You erase the contents of the dialog box that
suggests a new file name and put your new file
name in there. - If it contains PHP code, it has to end in .php.
9first PHP script
- Create a file with the name info.php, and the
following contents - lt?php
- phpinfo()
- ?gt
- nothing else. This will create a test page that
tells you everything PHP knows about. Look at
some of the variables.
10comment on info.php
- In terms of XML, the "lt?php" until "?gt" part is
called a processing instruction. It is a type of
node that we did not encounter in LIS650. - We can call any part of the file between "lt?php"
and "?gt" a PHP part of the file. - The XML file here contains just the processing
instruction.
11output of phpinfo()?
- phpinfo() create a whole web page for you, that
validates against a loose HTML specification. - That page contains a lot of technical detail.
- The section we may be interested in is PHP
Variables. It contains variables that we may be
interested in. These are variables that PHP can
understand - from its environment
- from the client
12the magic of PHP
- The client never sees the PHP code. It only sees
what the PHP processor has done with the code. - You can write normal HTML code, and you can
switch to writing PHP code pretty much at any
stage. - You can have several PHP parts.
- PHP parts can not be nested.
- The contents of the PHP part can be called a PHP
script.
13statements
- Like a normal text is split into sentences, a PHP
script is split into statements. - A PHP script contains one or more statements.
- Each statements tells the interpreter something.
- Each statement is ended by a semicolon.
- In our first script there is only one statement.
- Each statement is ended with a semicolon!
- Think of a statement like a rule in CSS. But
never forget the semicolon!
14expressions
- The stuff before the semicolon is called an
expression. - You can think of an expression as anything anyone
may want to write in a computer program. - So an expression is just a way to talk about
stuff in a program in a more edifying way than
just calling it stuff.
15functions
- phpinfo() is a function.
- Functions are one of the most fundamental
concepts in computer programming. - A function is an expression that does something
to something else. The something else is in the
parenthesis. It is called the argument of the
function. - The argument of phpinfo() is empty.
16second php script hello.php
- Normally we write HTML code and then we add PHP
parts. - Take validated.html, copy to hello.php
- make the body
- ltdivgt
- lt?php
- print("Hello, world!")
- ?gt
- lt/divgt
- Validate the resulting XHTML.
17comment on hello.php
- print() is also a function. print() prints its
argument. Here the argument is a string. A string
is a sequence of characters enclosed by single or
double quotes. - For print, the () can be omitted.
- You could have written three statements
- lt?php
- print "ltdivgt"
- print "Hello, world!"
- print "lt/divgt"
- ?gt
18good style
- Write each statement on a new line.
- Add plenty of comments. There are three styles of
comments in a PHP program - // the rest of the line is a comment
- the rest of a line is a comment
- / this is a comment /
- Only last style can be used over several lines.
- Do you recognize two of the commenting styles?
19another way to write hello.php
- lt?php
- greeting"Hello, world!"
- print "ltdivgtgreetinglt/divgt"
- ?gt
- Here greeting is a variable. The first statement
assigns it the string value "Hello, world!". The
second statement prints it out. - This example is important because it illustrates
the concept of a variable. - The name of the variable is greeting.
20variable names
- Variable name must start with a letter or
underscore. They can contain letters, digits and
underscores. The following are examples of
illegal names - 2drunk
- bottle-content
- brewer_at_grosswald
- Variable names are case sensitive. I use
lowercase only and add underscores in long names. - The variable name "this" is reserved.
- It is good to give variables meaningful names.
21strings
- a piece of text in PHP is called a string.
- A string is often surrounded by single quotes.
- print 'I want beer'
- want'beer'
- print 'I want want' // prints I want want
- If you want to use the values of variables, use
double quotes - want'beer'
- print "I want want"
- // prints I want beer
22single and double quotes
- You can use single quotes to quote double quotes
- print 'She wrote "I want beer." and sighed.'
- // prints She wrote "I want beer." and sighed.
- and vice versa
- print "She wrote 'I want beer.' and sighed"
- // prints She wrote 'I want beer.' and sighed.
- Sometimes it is not obvious when to put single
quotes, double quotes, and when to leave them
out. If one thing does not work, try something
else.
23the backslash escape
- The backslash is used to quote characters that
otherwise are special. - print 'Don\'t give me bad beer!'
- kind'bock'
- beer'Festbock'
- print "ltp class\"kind\"gtbeerlt/pgt"
- // prints ltp class"bock"gtFestbocklt/pgt
- The backslash itself is quoted as \\
- print "a \\ against beer consumption"
- // prints a \ against beer consumption
24more backslash escapes
- \n makes the newline character
- \r make the carriage return (no use in
Unix)? - \t makes the tab (seldomly used in
HTML)? - \ makes the dollar (used in the
shop)? - amount'1.50'
- print "you owe \amount per bottle."
- // prints you owe 1.50 per bottle.
- If the backslash was not there would be
considered to be a variable.
25concatenation
- This is done with the . operator. It puts two
strings together, the second one after the first
one. - cost'5.23'
- message'This costs ' . cost
- print message
- // prints This costs 5.23
26numbers
- Numbers are set without the use of quotes.
- You can , -, and / for the the basic
calculations. - There also is the modulus operator . It gives
the remainder of the division of the first number
by the second - print 10 7 // prints 3
- Use parenthesis for complicated calculations
- pack2 (10 7)
- print "a pack pack" // prints a 6 pack
27geeky increment/decrement
- is an operator that adds one. The value of the
resulting expression depends on the position of
the operator - a4
- print a // prints 5
- print a // prints 5
- b4
- print b // prints 4
- print b // prints 5
- -- works in the same way
28type conversion
- In some circumstance, PHP converts numbers to
strings and back. It works like magic. It
converts numbers to strings when required - one_in_three1/3
- print one_in_three // prints 0.333333333333
- and numbers to
- string"1.500"
- number3string
- print number
- Sometimes it converts to Boolean!
29Boolean value
- Every expression in PHP has a Boolean value.
- It is either 'true' or 'false'.
- In certain situation, an expression is evaluated
as a Boolean - For example
- if(expression)
- expression1 or expression2
30what is truth?
- All strings are true except
- the empty string
- the string "0"
- All numbers are true except
- 0
- 0.0
- example
- a5-4-1 // a is false
- Note that variables that you have not assigned
contents are false. This includes misspelled
variables!!
31isset()?
- isset() is a function that returns true if a
variable is set. - Under strict coding rules, that are enforced by
PHP running on wotan, the PHP processor will
issue a notice when you use a variable that has
not been set to a value. - isset(variable) can be used to find out if the
variable variable was set before using it.
32comparison operators
- Expressions that are evaluated in Boolean often
use comparison operators. - beer 'grosswald' // checks for equality
- Note difference from
- beer'grosswald' // this is always true
- Other comparisons are
- lt smaller than lt smaller or equal than
- gt larger than gt larger or equal than
33logical operators
- and is logical AND. or is logical OR.
- if(brand'Budweiser' or brand"Sam Adams")
- print "Commiserations for buying a lousy
beer\n" - where is the mistake in this piece of code?
- ! is Boolean NOT
- These can be combined. Use parenthesis
- if(((pints) gt 2 and (vehicle'car')) or
((pints gt 6) and (vehicle'bicycle'))) - print "order a cab!\n"
34if( condition )
- if( condition ) evaluates an expression condition
as Boolean, and executes a block of code
surrounded by curly brackets if the expression is
true. - if(drunk)
- print "Don't drive!\n"
-
- Note you don't need to indent the block as done
above, but the way Thomas has done it there is
pretty much standard, so do it in the same way.
35if( condition ) else
- if you have an if() you can add an else block of
code to execute when the condition is false - if(sober)
- print "You can drive\n"
-
- else
- print "Check if you are fit to drive\n"
36elseif( condition )
- You can build chain of conditions
- if(pints_drunk0)
- print "You are ok to drive\n"
-
- elseif(pints_drunklt3)
- print "Don't use the car, get on your bike\n"
-
- elseif(pints_drunklt6)
- print "Take a cab home\n"
-
- else print "Call the local hospital!\n"
37while( condition )
- while( condition ) executes a piece of code
while the condition condition is true - count0
- while(count lt 100)
- print "???? ??? ????? -- ?????? ??
?????!ltbr/gt" - countcount1 don't forget to increment
count! -
38getting back to forms
- Forms deliver data to the server. The server can
then process the data and deliver a response. - If the server process uses PHP, each control is
visible to PHP as a PHP variable. It can be read
into the script.
39control name and PHP variable
- When the form is passed to the PHP script named
with the action of the the ltformgt the controls
are accessible as PHP variables. - If name is the name of the control, and if the
method is POST, the control is read as the
variable _POST'name'. - If name is the name of the control, and if the
method is GET, the control is read as the
variable _GET'name'.
40example
- HTML file greet.html has
- ltform action"greet.php" method"get"gtltpgt
- your last name ltinput type"text"
name"lastname"/gtlt/pgtlt/formgt - PHP file greet.php has
- lt?php
- print "Hello "
- print _GET'lastname'
- ?gt
- in addition to the usual HTML stuff.
41iterative form input
- When users start to use your site, the shit hits
the fan. Users have many ways to do things wrong. - Many times you will have to print the form again,
with values already filled in. - In such circumstance a static HTML file for the
form is unsuitable. - Therefore we need a PHP file that writes out the
form and processes the form.
42check for submission
- We include a hidden element in the form to see if
it was submitted - ltinput type"hidden" name"submitted" value"1"/gt
- We start the script we check for submission
- if(_GET'submitted')
- // work on the data that was submitted
-
- else
- // print form
43master example greet.php
- lt?php
- if(_GET'submitted')
- name_GET'name'
- print "ltdivgtHello name!lt/divgt\n"
-
- else
- print "ltform action\"greet.php\"
method\"get\"gt\n" - print "ltdivgtltinput type\"hidden\" "
- print "name\"submitted\" value\"1\"
/gtlt/divgt\n" - print "ltpgtYour name ltinput type\"text\" "
- print "name\"name\" value\"name\" /gt"
- print "lt/pgtlt/formgt\n"
-
- ?gt
44PHP calling itself
- One cool thing to help with that is
- _SERVERPHP_SELF
- It gives the file name of your script in the
form. As - you change your script file name, you do not
need to change the name of the form submitted. - So in the previous slide, if you replace
- action\"greet.php\"
- with
- action\""._SERVER'PHP_SELF'."\"
- you can change the name of the PHP file. It will
still find the itself as the PHP file.
45http//openlib.org/home/krichel
- Thank you for your attention!
- Please switch off computers when you are done!