Title: PHP
1PHP
2Objectives
- What you should know already!
- Server side processing.
- Server side scripting.
- Server side scripting with PHP.
- Basic syntax.
- Passing parameters with a URL.
3Assumptions
- You know about the Internet and the Web.
- You know what a URL is and how HTTP works.
- You know how to write HTML.
- You know the difference between a web browser and
a web server. - You understand basic programming concepts.
4Server Side Processing
- Server side processing involves executing code on
the web server, before passing to the client. - The web browser never sees the code.
- E.g. PHP embedded in HTML.
- Contrast with client side processing, where the
web server passes code to the browser, which the
browser executes. - e.g. JavaScript embedded in HTML.
5Server Side Processing Server Side Scripting
- Server Side Scripting
- Program code embedded in HTML document.
- Web server processes code before passing HTML
document to the client. - Statements in the program code output HTML,
augmenting the surrounding HTML. - Note that the web server must understand the
scripting language. - E.g. An add-on module for the web server.
6Server Side Processing Server Side Scripting
- So server side scripting
- embeds program code in a HTML document.
- document is requested by a client (browser)
- web server pre-processes the document, running
any statements - statements generate additional HTML which is
pasted into the document - Augmented document is sent to client.
7Server Side Scripting With PHP
- PHP is a Server Side Scripting language.
- PHP stands for PHP Hypertext Pre-processor.
- Syntax very like C, with some Java and Perl
features. - Has built-in functions for database connection.
8PHP Script Structure
- PHP commands are embedded in standard HTML.
- ltpgt This is plain HTML.
- lt?php
- //Print something out
- echo ltbr /gt This is generated by PHP.
- ?gt
- ltbr /gt And this plain old HTML again!
- lt/pgt
9PHP Script Structure
- PHP statements are marked with start tag lt?php
and end tag ?gt. - Can use start tag lt?, but this might conflict
with other scripting languages. - Single line comments indicated by //.
- PHP ignores comments.
- Multi-line comments enclosed between / and
/. - Statements are terminated by a semi-colon .
10What The Browser Sees
- The web server pre-processes the above HTML,
executing the PHP statement echo. - The echo statement prints out the text
- ltbr /gt This is generated by PHP.
- So web browser is sent the following HTML
- ltpgt This is plain HTML.
- ltbr /gt This is generated by PHP.
- ltbr /gt And this plain old HTML again!
- lt/pgt
11String Literals
- String literals can be enclosed in single or
double quotes. - echo single quoted string
- echo double quoted string
- Can be useful
- echo She said Hello
- echo He wouldnt open it
- Or you can use escape sequence
- echo She said \Hello\
12Variables
- Variables names start with the dollar sign .
- Variable names are case sensitive.
- Variables dont need to be declared in advance.
- Variable types are assigned on instantiation.
13Variable Types
- PHP has four scalar types
- boolean
- float
- integer
- string
- PHP has two compound types
- array
- object
14Variables - Example
- Define a variable and give it a value.
- / Create a variable
- name var
- type integer
- value 15 /
- var 15
- Variables can change type
- var 15 //type integer
- var fifteen //type string
15Pitfalls Of Automatic Variable Typing
- Automatic variable declaration allows very
flexible coding, but can lead to problems. - E.g. Following is fine, but nothing is printed
out! - //Print a welcome message
- welcomeMsg Hello .userName
- echo welcomemsg
16Constants
- Constants are simple, scalar values.
- //Define a constant max
- define(max, 100)
- Once defined, you can use a constant anywhere in
the script.
17Arithmetic Expressions and Operators
- PHP has the usual assignment and arithmetic
operators. - //Assign the value 4 7 to var
- var 4 7
- E.g. Temperature conversion.
- F 84 //Fahrenheit
- C 5((F-32)/9) //Celsius
18Arithmetic Expressions and Operators
- Also has increment operators.
- //All add one to var
- var var 1
- var 1
- var
- Similarly for other arithmetic operators (see the
PHP manual).
19String Operators
- Can concatenate (join together) strings.
- name Fred
- echo Hello . name
- Outputs the following
- Hello Fred
20Type Conversion
- PHP will automatically convert between types if
the code requires. - E.g. Output an integer as part of a string.
- var 15
- echo The number is . var
- The integer variable var is automatically
converted to a string. The output will be - The number is 15
21Type Conversion
- You can also explicitly cast a variable from one
type to another. - E.g. Cast an integer to a string.
- var 15
- echo Number is .(string) var
- Type conversion usually follows common sense, but
see PHP Manual for details.
22Checking Variable Type
- Because variables can change type in PHP, its
useful to test their current type before using
them. - boolean is_int(mixed variable)
- boolean is_float(mixed variable)
- boolean is_bool(mixed variable)
- boolean is_string(mixed variable)
- Return true if variable is the correct type.
23Basic Debugging
- To print the type, and contents, of a variable
you can use the functions. - print_r(mixed expression)
- var_dump(mixed expression , mixed expression
) - Print out a readable description of any type of
variable, including arrays and objects.
24PHP Control Structures
PHP Control Structures
25If else Statement
- Follows fairly standard format
- if (var gt 15)
-
- echo Greater than 15
-
- else
-
- echo Not greater than 15
26If else Statement
- There is no end if.
- The else part is optional.
- The braces are only really needed to enclose
multiple statements. For example, this is OK - if (var gt 15)
- echo Greater than 15
- else
- echo Not greater than 15
27If elseif Statement
- Consecutive conditions can be tested
- if (var gt 15)
- echo Greater than 15
- elseif (var 15)
- echo Equal to 15
- else
- echo Less than 15
28Multiple Conditions
- The condition may have several parts, joined by
Boolean operators. - Or is written
- And is written
- if (var gt 15) (var lt 20)
- echo Between 15 and 20
- See PHP manual for details of conditional
expressions.
29Loops
- PHP has four loop statements
- while
- do while
- for
- foreach
- foreach introduced in PHP4 to make it easier to
iterate through an array (later lecture ).
30while Loops
- Loops through a block of statements until the
end-loop condition is met. - E.g. List the integers from 1 to 10.
- counter 1
- while (counter lt 11)
-
- echo counter .
- counter //add 1 to value of counter
-
- Prints out 1 2 3 4 5 6 7 8 9 10
31while and do while Loops
- The end-loop condition is evaluated first, so if
it is not met, the loop body is never executed. - The do while loop executes the loop body before
testing the end-loop condition. - See Text book OR PHP online manual for details.
32for Loops
- Loops through a block of statements a fixed
number of times. - E.g. List the integers from 1 to 10.
- for(counter1 counterlt11 counter)
-
- echo counter .
-
- Same result as the while loop above, but more
compact.
33The break Statement
- You can stop a loop before the end-loop condition
is met using the break statement. - E.g. Stop listing the integers at 6.
- for(count1, stop6 countlt11 count)
- if (countstop)
- break
- echo count .
-
- Also illustrates multiple initialisation
statements.
34The continue Statement
- You can also skip to the next iteration using the
continue statement. - E.g. List the integers from 1 to 10, except 6.
- for(count1, skip6 countlt11 count)
-
- if (countskip)
- continue
- echo count .
-
35Formatting Strings With printf()
- The echo statement provides a basic method for
outputting text. - To provide more sophisticated formatting, use the
printf function - integer printf(string format , mixed args
) - printf() sends its output direct to the PHP
output buffer used to build the HTTP response.
36printf() Example
- Accept a floating point number var and print out
its value. - printf(Result f, var)
- The f symbol is a conversion specification. It
tells printf to expect an argument of type float
as the second parameter (our var) and to output
the value of this parameter as part of the string.
37printf() Conversion Specifications
- As well as f for float we have
- b for boolean.
- c for single character.
- d for integer.
- s for string.
- We can also include a width specifier.
- 5.2f means display the number with five digits
before and two after the decimal point.
38printf() Example
- Now print out var to two decimal places.
- var 3.14159
- printf(Result 5.2f, var)
- The actual output will be
- Result 3.14
- There are an additional 4 spaces (for clarity,
indicated by the character) before the 3! - Result 3.14
39Comparing Strings strcmp()
- Using the equality operator will not
always correctly compare two strings. - Use the function strcmp() to do this.
- integer strcmp(string str1, string str2)
- There is also a strncmp() to compare the first n
characters of the strings - see the PHP online
manual!
40strcmp() Example
- strcmp() returns and integer
- 0 if the strings are the same
- -1 if str1 precedes str2 alphabetically
- and 1 if str2 precedes str1 alphabetically.
- For example
- strcmp(apple, pear) //-1
- strcmp(apple, apple) //0
- strcmp(pear, apple) //1
41Other Functions To Manipulate Strings
- PHP has lots of string manipulation functions.
There are functions to - Pad strings
- Change case
- Trim white space
- Find one string in another
- and lots more. See the PHP manual for details!
42Passing Parameters To PHP Scripts
- There are three ways of passing a parameter to a
PHP script. - Type the URL manually, and add the parameter and
its value to the end. - Embed a hyperlink in your HTML that includes the
parameter and its value. - Use a HTML form to capture the parameters value.
43Passing Parameters With The URL
- Consider this URL.
- http//localhost/example1.php?userFred
- Specifies a HTTP request,
- For a file on the local host called example.php,
- The ? indicates the start of the parameter
list. - When PHP sees user Fred, it automatically
adds an element user with value Fred to the
associative array _GET (will be explained later).
44Using Parameter Inside The PHP Script
- Consider this fragment of a script file called
with URL http//localhost/example1.php?userFre
d - ltbodygt
- ltpgt
- lt?php
- echo Hello . _GETuser . \n
- ?gt
- lt/pgt
- To use the value of a URL parameter, we just need
to know its name.
45Passing Multiple Parameters
- Consider this URL.
- http//localhost/example2.php?userFredage36
- ltbodygt
- ltpgt
- lt?php
- echo Hello ._GETuser.\n
- echo You are ._GETage. years old
- ?gt
- lt/pgt
46Embedded Hyperlinks
- The same basic technique can be used to generate
as hyperlink with a parameter list added to the
URL. - Simply use the printf or echo statements to
generate the URL.
47Reading
- Online PHP Manual.
- PHP books mentioned in Module Guide.