Title: Dr. Natalio Krasnogor
1G64HLL High-Level Languages Lecture 5
- Dr. Natalio Krasnogor
- Natalio.Krasnogor_at_Nottingham.ac.uk
2What were going to look at
- This is the shortest and easiest section of HLL
- Were going to look at
- What the heck PHP is
- Why PHP is so bloody great
- Why its (in some cases) preferable to
JSP/ASP..(even perl) - How to code properly in PHP
- How to use PHP to create web applications/database
s..
3Short Learning Curve
- For a long time, the primary language used to
develop such Web sites was Perl. - Ask any novice programmer, and he'll tell you
that learning Perl isn't exactly a bed of
roses... - There has been a proliferation of alternative
server-side scripting languages that have a
shorter learning curve. - PHP has the unique distinction of being an
open-source server-side scripting language that's
both simple, powerful and easy to learn.
4Recommended Reading
- PHP and MySQL Web
- Development
- (1st or 2nd edition)
-
- Luke Welling,
- Laura Thomson
- 25.55 (Amazon.co.uk)
5References Web
- http//www.firepages.com.au/phpdev4.htm
- A great php server (install it)
- http//www.php.net
- The best Php site
- http//www.phpfreaks.com
- http//www.phpbuilder.com
6A choice of editing programs
- Windows - JFE
- UNIX - Nedit
- Make sure you check
- The sh-bang ! on the university systems
- The sh-bang is not there if its not needed
- The security on the php file (chmod 755 )
- That you have the php in the correct folder
71. PHP HISTORY
- PHP is a language for creating interactive web
sites. It was originally called "Personal Home
Page Tools" when it was created in 1994 by
Rasmus Lerdorf to keep track of who was looking
at his online CV. - Mid-1997 Technion (Israel) students Andi Gutmans
and Zeev Suraski redesigned the PHP language
engine and wrote some of the most popular PHP
modules. - At that time PHP already had its own site,
php.net, run by the computer science community,
and was powering thousands of Web sites.
8Recursive Names
- PHP originally stood for
- Personal Home Pages.
- PHP is now a recursive acronym that stands for
- PHP Hypertext Preprocessor
9These Dates are recent
- Version 0.0 Conceived in autumn of 1994
- Version 1.0 Personal Home Page Tools in early
1995 - Version 2.0 PHP/FI 1995-1997
- Version 3.0 PHP 1997-2000
- Version 4.0 PHP mid-2000
- Version 4.1 10 Dec 2001
- Version 4.2 22 Apr 2002
- Development Team (515 people with CVS commit
access). - Today PHP Web developers applaud the script's
simplicity, flexibility, and ability to generate
HTML pages quickly. - At start of 2001 gt 5,100,000 sites around the
world used PHP.
10PHP Usage May 2002
11Hello World!
- Hello World in Pascal
- program hello
- begin
- writeln('Hello World')
- end.
12Hello World!
- Hello World in Java
- class hello
-
- public static void main(int args, char argv)
-
- System.out.println("Hello World")
-
13Hello World!
- Hello World in Perl
- print "Hello World"
-
- Hello World in PHP
- Hello World
-
142. PHP Style
- PHP Style Programming
- ltHTMLgt
- ltHEADgt
- ltTITLEgtSearch results for "lt?php print
query?gt"lt/TITLEgt - lt/HEADgt
- ltBODYgt
- Traditional Perl Programming
- !/usr/bin/perl
- print "ltHTMLgtltHEADgt\n"
- print "ltTITLEgtSearch results for
\"query\"lt/TITLEgt\n" - print "lt/HEADgt\n"
- print "ltBODYgt\n"
15An Embedded Script?
- You could view PHP as an embedded language.
- Unlike perl which can print HTML out PHP does
this by default. - However it is very much still a Server Side
Language this is not the same sort of thing as
Javascript
16PHP Tag Styles The bad
- Script Style
- ltSCRIPT LANGUAGEphpgt
- print this is script style
- lt/SCRIPTgt
- ASP Style
- lt
- print this is ASP style
- gt
- Both these styles of opening code is perfectly
valid but they are based on other languages.
17PHP Tag Styles The good
- XML Style
- lt?php
- print this is XML style
- ?gt
- Short Style
- lt?
- print this is ASP style
- ?gt
- To use Short style the PHP you are using must
have short tags enabled in its config file
this is almost always the case.
18Switching Modes
- lt?
- if(strstr(HTTP_USER_AGENT,"MSIE"))
-
- ?gt
- ltbgtYou are using Internet Explorerlt/bgt
- lt?
-
- else
-
- ?gtltbgtYou are not using Internet Explorerlt/bgt
- lt?
-
- ?gt
19PHP is C Style
- PHP, just as perl is, is very similar to C.
- This is because C is top banana.
- As a consequence if you know java (also a c
clone), C or indeed almost any other computer
science language you pretty much already know
PHP. - However more than anything PHP is based on Perl.
20Creating a PHP program
- You can use any text editor
- Different server setups work in different ways
- University start with the good old sh-bang
- Most other systems on the web know that if your
file has a .php extension you are using PHP. - Use lt? To open your code and ?gt to close it.
- Anything not in those tags is rendered as HTML.
- Type your code and load it up in a web browser.
21Running your PHP program
- The ! Line for PHP on the CS machines is
- !/usr/bin/php
- You'll also need to tell UNIX that your file is
executable, by typing an appropriate chmod line
into the UNIX shell - chmod 755 hello.php
- If just typing the filename doesn't run the
program, try - ./filename
22Comments
- Why do we go on about comments so much?
- because without them, marking would hurt our
heads is GOOD PROGRAMMING PRACTICE. - lt? //C style comment
- perl style comment / C
multi line comment / - ?gt
23God, please, no more Hello World programs
- Hello World now looks like
- !/usr/bin/php
- ltHTMLgt
- Hello World
- lt?
- print Is it meeeee your looking foooor\n"
- // Hello World - By Jim and Lionel Ritchie
- ?gt
- lt/HTMLgt
244. PHP is for Lazy People
- Yup, you know that means you!
- Basic data types
- numbers (integers and real)
- strings (Double-quoted "abc and single-quoted
'abc' ) - booleans (true,false )
- Dynamic typing
- Don't have to declare types
- Automatic conversion done
25Variables in action
- lt? x false // boolean x true
x 10 // decimal x 1.45
// Floating point x 0x1A // hexade
cimal x "mmm\"oo'oo\n" // mmm"oo'oo a
nd a new line x 'mmm"oo'oo\n' // string
mmm"oo'oo\n y x // Reference
x1 10 // array of decimals
x"name" jimbo" // associative array
x2"lala" "xx" // a two dimensional
array - ?gt
26Printing out Variables
- lt?
- name Winnie the Pooh" friends array(don
key" gt Ee-or" , tiger" gt tigger") - print friendsdonkey
- print hid all of
- print name s weapons of mass destruction
- ?gt
- OUTPUT
- Ee-or hid all of Winnie the Pooh s Weapons of
mass destruction
27Type conversion
- Conversion between types can be pure or dirty
- forced casting
- lt?
- bool true print (int)bool
- ?gt
- Type Juggling
- lt? x "100" x
// x is now 101 ?gt
28Variable Scope
- The 3 basic types of scope in PHP are
- Global variables declared in a script are visible
throughout that script, but not inside functions - Variables used inside functions are local to the
function - Variables used inside functions that are declared
as global refer to the global variable of the
same name
29Numerical functions as per normal
- Addition
- a 1 1 // sets a to 2
- a 4 // adds 4 to a
- a // adds 1 to a
- Subtraction
- a 10 - 5 sets a to 5
- a - 6 subtracts 6 from a
- a-- subtracts 1 from a
30Final Numerical functions
- Multiplication
- a 2 3 // sets a to 6
- a 10 // multiplies a by 10
- Division
- a 10 / 3 // sets a to 3.3333
- a / 2 // halves a
- Modulus
- a 10 3 // sets a to 1
- a 2 // sets a to modulus 2
- // of itself
31PHP Operators
- Addition - Subtraction
Multiplication / Division Modulus
And Or Xor . add string (like in
perl) ltlt Shift left gtgt Shift right
32Simple String functions
- chr - Return a specific character
- ord - Return ASCII value of character
- sprintf - Return a formatted string
- strlen - Get string length
- strpos - Find position of first occurrence of a
string - strrev - Reverse a string
- strtolower - Make a string lowercase
- strtoupper - Make a string uppercase
- str_replace - Replace all occurrences of the
search string with the replacement string
33Deja Vu
- Just for the record whats the same as C/Java?
- For Loops
- While Loops
- If Statements
- Break, Continue, Exit, Switch, etc.
- The main concepts which differ in syntax are
- Functions
- Classes
34More déjà vu If statements
- if statements
- if (some test)
-
- // code here
-
- else if (some other test)
-
- // code here
-
- else
-
- // if neither test met, do this
35Conditional tests
- Equals
- gt Greater than
- lt Less than
- gt Greater or equal to
- lt Less or equal to
- ! Not equal
- This, unlike perl, works exactly the same way for
strings as it does for integers and floats.
36If exists
- You often need to check whether a variable exists
in PHP. - There are two ways to do this
if (a) print \a exists"
if (!empty(a)) print \a exists"
37Foreach loops Useful things
- lt? //valuesa array (1, 2, 3, 17)
foreach (a as v) - print "Current value of \v v.ltbrgt"
// values and keys a array (1, 2, 3, 17)
foreach(a as k gt v) - print "\ak is vltbrgt"
- ?gt
38Mixing up types in Arrays
- lt?
- arr array(1,'foo',1.57,'cat'gt'mouse','dog'gt'
mailman') - ?gt
- lt? foreach( arr as kgtv )
- print "\arrk vltbrgt\n"
- ?gt
Setting up an array
OUTPUT arr0 1arr1 fooarr2
1.5arrcat mousearrdog mailman
39PHP Functions
- Functions in PHP are not case sensitive.
- Be careful of this because variable naming is
case sensitive - function my_function()
-
- print My function was called
-
- Functions can be created anywhere in your PHP
code - However good style demands they should always be
at the top of your code
40Do what is neatest
- lt?
- Function my_function()
-
- print My function was called
-
- ?gt
lt? Function my_function() ?gt My function
was called lt? ?gt
IS IDENTICAL TO
41Passing Parameters
- As normal you dont have to specify the types of
your parameters... - Be careful of this because variable naming is
case sensitive - function display_table(data)
-
- print ltTABLEgt
- for (i0 iltsizeof(data) i)
- print ltTRgtltTDgt datai lt/TDgtlt/TRgt
- print lt/TRgt
-
42Defaults Passing by Reference
- As with other languages you can set defaults. PHP
requires that you do specify the correct number
of parameters in a call. - Equally you dont have to pass by value an
ampersand will mean the variable is passed by
reference and so any changes to it are global - function increment(value, amount1)
-
- value value amount
-
43Returning Stuff
- The keyword return stops the execution of a
function. - You can have as many return statements as you
like at any point in your function the first
one encountered in the program flow will end the
function. - To return a value just add it after the keyword
again any type can be returned. - You dont need to specify a return type in the
function declaration
44Example
- function larger(x, y)
-
- if (x gt y)
- return x
- if (x lt y)
- return y
- if (x y)
- return x and y have the same value
45Object Orientation
- Much better in PHP 4 than its previous version
- However still rather simplified
- class classname
-
- var attribute1
- var attribute2
- function method1()
-
- No real data encapsulation.
- So if you DO go object then be consistent and
dont violate the encapsulation.
465. PHP or ASP?
- Open source
- PHP is faster
- Superior Memory Management
- Tied up with GET and POST variables are
automatically created as global variables - Truly Portable
- Less Coffee Breaks
- Less cursing Microsoft
- PHP is free!?!
Need ASP email management? buy ServerObject's
QMail. Need ASP file uploading? -- buy Software
Artisans SA-FileUp. Need ASP encryption? -- buy
ASPEncrypt.
47What is PHP bad at?
- Cant really think of anything
- Sometimes hard to debug because it does so much
for you - Thats it.
48Summary
- PHP is easy. It does most things for you.
- From now on we will be looking at why it is Good
as opposed to simple. - Its tricks, features and functions are excellent.
49Thats All Folks