Title: Scripting Languages and PHP
1Scripting Languages and PHP
2Origins
- Rasmus Lerdorf
- Personal Home Pages.
- Zeev Surashi and Andi Gutmans
- Open Source
- PHP version 4
- Full information on which facilities installed
phpinfo()
3PHP as a programming language
- Evolved not designed
- Cross-platform
- Scripting language
- designed to support a specific task
- untyped variables (but values are typed)
- implicit variable declaration
- implicit type conversion
- stored only as script files
4- Procedural (not event driven)
- -runs from start to finish
- Procedural (not object-oriented)
- Library of function not classes
- C-style language
5- Free format - white space is ignored
- Statements are separated by semi-colon
- Statements grouped by
- Comments begin with // or a set of comments /
/ - Assignment is a6
- Relational operators are ,lt , gt ( not a
single equal) - Control structures include if (cond) .. else
, while (cond) .. , for(sstartcond,
increment, endcond) - Arrays are accessed with x4 is the
5th element of the array x indexes start at 0 - Associative Arrays (or hash arrays in Perl,
sparse arrays in data structures) are accessed in
the same way yfred - Functions are called with the name followed by
arguements in a fixed order enclosed in ( )
substr(fred,0,2) - Case sensitive - fred is a different variable
to FRED
6- HTML-embedded
- PHP scripts are essentially HTML pages with the
occasional section of PHP script. - PHP script is enclosed in the tag pair
- lt?php and ?gt
- Scripts may be short
- lth1gtDepartures from lt?php echo date( Hi ) ?gt
lt/h1gt - or long
- (the rest of the program)
7Server-side
PHP processor
Web server
Browser
get Myprog.php?a8bfred
Locate file Determine its PHP Set up context URL
variables etc
Interpret script Access context
Script output
HTML
8PHP-Server interaction
- Accessing the input data
- Parameters in the URL become variables of the
same name. a6 bfred - Parameters in a array _GETa
- Accessing Environment Variables
- PHP_SELF is the program name
- HTTP_USER_AGENT is the browser
- phpinfo() to list all of them installed
libraries - Producing Output
- lth1gtAnnouncementslt/h1gt
- lt?php print(lth1gtAnnouncementslt/h1gt) ?gt
- lt?php print lth1gtAnnouncementslt/h1gt ?gt
- lt?php echo lth1gtAnnouncementslt/h1gt ?gt
9User defined functions
- function double(n)
- return n2
-
- which we can use as
- x double(y)
- or
- xdouble(4)
- place common functions in a separate file and
include it - include(myfuns.php)
10PHP tasks
- Data
- Script variables - a
- Session variables later
- Persistant use a database
11String Handling
- String literals (constants) enclosed in double
quotes or single quotes - Within , variables are replaced by their value
called variable interpolation. My name is
name, I think - Within single quoted strings, interpolation
doesnt occur - Strings are concatenated (joined end to end) with
the . operator key.board is keyboard - Standard functions exist strlen(), substr() etc
- Regular expressions be used for complex pattern
matching - Values of other types can be easily converted to
and from strings numbers implicitly converted
to strings in a string context.
12Function library
- Basic tasks
- String Handling
- Mathematics random numbers, trig functions..
- Regular Expressions
- Date and time handling
- File Input and Output
- And more specific functions for-
- Database interaction
- MySQL, Oracle, Postgres, Sybase, MSSQL ..
- Encryption
- Text translation
- Spell-checking
- Image creation
- XML
13lthtmlgtltheadgtlttitlegtBus departures from
Frenchaylt/titlegtltmeta http-equivrefresh
content60gtlt/headgtltbodygtlth1gtFrenchay
Buseslt/h1gtlth2gtlt?php echo date ("Hi") ?gtlt/h2gt
14lt?php// form variables none// connect to the
database server "shares.cems.uwe.ac.uk"
user"cwstudent" password"cwstudent" d
b"Bus" if(!(dblinkmysql_connect(server,u
ser,password))) print("mysql_connect
failedltbrgt\n") query "select from
departure where dtime gt current_time() order by
dtime limit 10" if(!(dbresult mysql_db_que
ry(db,query,dblink))) print("ltbgtquery
failedlt/h4gtltbrgt\n") print(mysql_error()
. "ltbrgt\n")
15 print("lttable border1gt") print("lttrgtltthgtRo
utelt/thgtltthgtTimelt/thgtlt/trgt") while(departure
mysql_fetch_object(dbresult)) print("ltt
rgt") print("lttdgtdeparture-gtroutenolt/tdgt")
print("lttdgtdeparture-gtdtimelt/tdgt")
print("lt/trgt\n") print("lt/tablegt"
) // close the database mysql_close(dblin
k)?gtlt/bodygtlt/htmlgt