Presentation is loading. Please wait.

Presentation is loading. Please wait.

Scripting Languages and PHP. Origins Rasmus Lerdorf ‘Personal Home Pages’. Zeev Surashi and Andi Gutmans Open Source PHP version 4 Full information on.

Similar presentations


Presentation on theme: "Scripting Languages and PHP. Origins Rasmus Lerdorf ‘Personal Home Pages’. Zeev Surashi and Andi Gutmans Open Source PHP version 4 Full information on."— Presentation transcript:

1 Scripting Languages and PHP

2 Origins Rasmus Lerdorf ‘Personal Home Pages’. Zeev Surashi and Andi Gutmans Open Source PHP version 4 Full information on which facilities installed: phpinfo()

3 PHP 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 ‘=’: $a=6 Relational operators are, == ( not a single equal) Control structures include if (cond) {..} else { }, while (cond) {.. }, for(sstartcond, increment, endcond) { } Arrays are accessed with [ ] : $x[4] 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: $y[“fred”] 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: Scripts may be short: – Departures from or long : (the rest of the program)

7 Server-side Browser Web server get Myprog.php?a=8&b=fred Locate file Determine its PHP Set up context– URL variables etc PHP processor Interpret script Access context Script output HTML

8 PHP-Server interaction Accessing the input data –Parameters in the URL become variables of the same name. $a==6 $b==‘fred’ –Parameters in a array $_GET[‘a’] 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 – Announcements – Announcements ”) ?> – Announcements ” ?>

9 User defined functions function double($n) { return $n*2 } which we can use as – $x = double($y) or – $x=double(4) place common functions in a separate file and include it –include(“myfuns.php”)

10 PHP tasks Data –Script variables - $a –Session variables – later –Persistant – use a database

11 String 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 doesn’t 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.

12 Function 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

13 Bus departures from Frenchay Frenchay Buses

14 \n"); } $query= "select * from departure where dtime > current_time() order by dtime limit 10"; if(!($dbresult = mysql_db_query($db,$query,$dblink))) { print(" query failed \n"); print(mysql_error(). " \n"); }

15 print(" "); print(" Route Time "); while($departure=mysql_fetch_object($dbresult)) { print(" "); print(" $departure->routeno "); print(" $departure->dtime "); print(" \n"); } print(" "); // close the database mysql_close($dblink); ?>


Download ppt "Scripting Languages and PHP. Origins Rasmus Lerdorf ‘Personal Home Pages’. Zeev Surashi and Andi Gutmans Open Source PHP version 4 Full information on."

Similar presentations


Ads by Google