140 likes | 154 Views
Intermediate PHP (1) Data Structures, Functions, Basic OO & Error Handling. Pre-amble (1) Web 1.0. Basic web 1.0 architecture & communication. Pre-amble (2) http request/response cycle. Pre-amble (3) PHP Origins.
E N D
Intermediate PHP (1)Data Structures, Functions, Basic OO & Error Handling
Pre-amble (1) Web 1.0 Basic web 1.0 architecture & communication
Pre-amble (3) PHP Origins Originally created by Rasmus Lerdorf (born Greenland, educated in Canada) around 1995 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
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)?
Preamble (5) C-like language • 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
Note: if you leave out the index number, php will start at 0 and use consecutive integers thereafter. 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).<br/>”); ?> Output?
PHP Data Structures (2) arrays (cont.) example: counting and accessing values using a loop <?php $teacher[] = “Prakash”; $teacher[] = “Paul”; $teacher[] = “Dan”; $teacher[] = “Chris”; $indexCount = count ($teacher); for ($index=0; $index < $indexCount; $index++) { print(“Teacher $index is $teacher[$index]. <br/>”;) } ?> Output?
PHP Data Structures (3) arrays (cont.) Associative Arrays (Hashes in Perl, HashMaps in Java) <?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.<br/>”); } ?> Output?
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
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.
3-tier architecture voice DHTML HTTP SQL touch PHP script Browser (IE, FireFox, Opera) Database Web Server (Apache, IIS) Database Server vision HTML tables Desktop (PC or MAC) SMS Web Service SMS system Client application Remote services
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
PHP Resources • hand out : - php home : http://www.php.net • learning PHP : php @ w3 schools • code and examples : php extension & application library (pear) : http://pear.php.net/ resource index : http://php.resourceindex.com/ weberdev : http://www.weberdev.com/ArticlesSearch/Category/Beginner+Guides phpexample : http://phpexamples.me/