Presentation is loading. Please wait.

Presentation is loading. Please wait.

Intermediate PHP (1) Data Structures, Functions, Basic OO & Error Handling.

Similar presentations


Presentation on theme: "Intermediate PHP (1) Data Structures, Functions, Basic OO & Error Handling."— Presentation transcript:

1 Intermediate PHP (1) Data Structures, Functions, Basic OO & Error Handling

2 Pre-amble (1) Web 1.0 Basic web 1.0 architecture & communication

3 Pre-amble (2) http request/response cycle

4 Pre-amble (3) PHP Origins Originally created by Rasmus Lerdorf (born Greenland, educated in Canada) around 1995Rasmus Lerdorf PHP originally abbreviated as ‘Personal Home Pages’, now known as ‘PHP Hypertext Preprocessor’ Other key developers: Zeev Surashi and Andi Gutmans (Israel) responsible for PHP 3.0 - a complete re-write of the original PHP/F1 engine (1997) PHP is Open Source software First version PHP/FI released 1995 PHP 5.0 released July 2004 Current version is 5.3.0 PHP version 5.2.4 current at UWE PHP Version 6 due for release late 2009 / early 2010

5 Preamble (4) PHP as a scripting language A scripting language is: –often evolved not designed –cross-platform since interpreter is easy to port –designed to support a specific task – PHP Web support –un-typed variables (but values are typed) –implicit variable declaration –implicit type conversion –stored only as script files –compiled on demand –may run on the server (PHP, Perl) or the client (Javascript) What are the potential differences in programming using an interpreted scripting language like PHP in place of a compiled language (e.g. Java in JSP,.NET)?

6 Free format - white space is ignored Statements are terminated 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(startcond; increment; endcond) { } Arrays are accessed with [ ] - $x[4] is the 5th element of the array $x – indexes start at 0 Associative Arrays (hash array in Perl, dictionary in Java) are accessed in the same way: $y[“fred”] Functions are called with the name followed by arguments in a fixed order enclosed in ( ) : substr(“fred”,0,2) Case sensitive - $fred is a different variable to $FRED Preamble (5) C-like language

7 PHP Data Structures (1) arrays - arrays collect values into lists - individual items (elements) are accessed using an index which can be an integer or a string - you can build arbitrarily complex data structures using arrays within arrays, that is, a value within an array can hold another array (multidimensional array) single dimension arrays - use square brackets [ ] to refer to an element - arrays are treated like normal variables – you can create it at the point of use without having to declare anything first. example: referencing array elements <?php $teacher[0] = “Prakash”; $teacher[1] = “Paul”; $teacher[2] = “Dan”; $teacher[3] = “Chris”; echo (“$teacher[3] teaches DSA). ”); ?> Output? Note: if you leave out the index number, php will start at 0 and use consecutive integers thereafter.

8 <?php $teacher[] = “Prakash”; $teacher[] = “Paul”; $teacher[] = “Dan”; $teacher[] = “Chris”; $indexCount = count ($teacher); for ($index=0; $index < $indexCount; $index++) { print(“Teacher $index is $teacher[$index]. ”;) } ?> PHP Data Structures (2) example: counting and accessing values using a loop arrays (cont.) Output?

9 <?php //fill in some info $user[“Name”] = “Leon Trotsky”; $user[“Location”] = “Coyoacan, Mexico City”; $user[“Occupation”] = “Revolutionary”; //loop over the values foreach ($user as $key=>$value) { print ($key is $value. ”); } ?> Output? PHP Data Structures (3) arrays (cont.) Associative Arrays (Hashes in Perl, HashMaps in Java)

10 Function library (700+) 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

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 dot operator “key”.”board” == “keyboard” Standard functions exist: strlen(), substr() etc Values of other types can be easily converted to and from strings – numbers implicitly converted to strings in a string context. Regular expressions be used for complex pattern matching.

12 3-tier architecture PHP script Remote services Web Server (Apache, IIS) Browser (IE, FireFox, Opera) Desktop (PC or MAC) Database Server SQL Client application HTTP HTML Web Service tables DHTML SMS vision touch voice SMS system

13 Learning PHP Start with just the basics, installing a script to output an HTML page Understand how PHP supports interaction with the Browser or other clients Understand how PHP supports integration with databases – MySQL Understand how PHP supports integration with other applications – Web services

14 PHP Resources -hand out : -php home : http://www.php.nethttp://www.php.net -learning PHP : php @ w3 schoolsphp @ w3 schools -code and examples : php extension & application library (pear) : http://pear.php.net/ http://pear.php.net/ resource index : http://php.resourceindex.com/http://php.resourceindex.com/ weberdev : http://www.weberdev.com/ArticlesSearch/Category/Begi nner+Guides http://www.weberdev.com/ArticlesSearch/Category/Begi nner+Guides phpexample : http://phpexamples.me/http://phpexamples.me/


Download ppt "Intermediate PHP (1) Data Structures, Functions, Basic OO & Error Handling."

Similar presentations


Ads by Google