comp 205 - week 5 dr. chunbo chu. agenda 1. brief history of php 2. basics 3. advanced

84
PHP: Hyper-text Preprocessor COMP 205 - Week 5 Dr. Chunbo Chu

Upload: kristopher-barber

Post on 26-Dec-2015

219 views

Category:

Documents


0 download

TRANSCRIPT

  • Slide 1
  • COMP 205 - Week 5 Dr. Chunbo Chu
  • Slide 2
  • Agenda 1. Brief History of PHP 2. Basics 3. Advanced
  • Slide 3
  • Brief History of PHP PHP (PHP: Hypertext Preprocessor) was created by Rasmus Lerdorf in 1994. It was initially developed for HTTP usage logging and server-side form generation in Unix. PHP 2 (1995) transformed the language into a Server-side embedded scripting language. Added database support, file uploads, variables, arrays, recursive functions, conditionals, iteration, regular expressions, etc. PHP 3 (1998) added support for ODBC data sources, multiple platform support, email protocols (SNMP,IMAP), and new parser written by Zeev Suraski and Andi Gutmans. PHP 4 (2000) became an independent component of the web server for added efficiency. The parser was renamed the Zend Engine. Many security features were added. PHP 5 (2004) adds Zend Engine II with object oriented programming, robust XML support using the libxml2 library, SOAP extension for interoperability with Web Services, SQLite has been bundled with PHP
  • Slide 4
  • Brief History of PHP As of July 2007, PHP is used on 20,917,850 domains, 1,224,183 IP addresses. http://www.php.net/usage.php Source: Netcraft http://www.php.net/usage.phpNetcraft
  • Slide 5
  • Why is PHP used? 1.Easy to Use Code is embedded into HTML. The PHP code is enclosed in special start and end tags that allow you to jump into and out of "PHP mode". Example
  • Slide 6
  • echo is the common method in outputting data. Since it is a language construct, echo doesnt require parenthesis like print(). Output Text Usage: // prints out Hello World Output the value of a PHP variable: // prints out the number of hits Echo has a shortcut syntax, but it only works with the short open tag configuration enabled on the server. How to output using PHP
  • Slide 7
  • Examples 3. Other uses with echo Automatically generate the year on your pages. This will print out 2009 UC Riverside. UC Riverside You will need to escape any quotation marks with a backslash.
  • Slide 8
  • Why is PHP used? 2.Cross Platform Runs on almost any Web server on several operating systems. One of the strongest features is the wide range of supported databases Web Servers: Apache, Microsoft IIS, Caudium, Netscape Enterprise Server Operating Systems: UNIX (HP-UX,OpenBSD,Solaris,Linux), Mac OSX, Windows NT/98/2000/XP/2003 Supported Databases: Adabas D, dBase,Empress, FilePro (read-only), Hyperwave,IBM DB2, Informix, Ingres, InterBase, FrontBase, mSQL, Direct MS-SQL, MySQL, ODBC, Oracle (OCI7 and OCI8), Ovrimos, PostgreSQL, SQLite, Solid, Sybase, Velocis,Unix dbm
  • Slide 9
  • Why is PHP used? 3.Cost Benefits PHP is free. Open source code means that the entire PHP community will contribute towards bug fixes. There are several add-on technologies (libraries) for PHP that are also free.
  • Slide 10
  • Server-side Script User-agent (web browser) requests a web page http request Server detects PHP code in page, executes the code, and sends the output to the user http response Web page (with PHP Output) sent to PC User never sees the PHP, only the output Cannot affect the browser or client PC
  • Slide 11
  • Getting Started 1. How to escape from HTML and enter PHP mode PHP parses a file by looking for one of the special tags that tells it to start interpreting the text as PHP code. The parser then executes all of the code it finds until it runs into a PHP closing tag. PHP CODE HTML
  • Slide 12
  • Start your Virtual Machine
  • Slide 13
  • Getting Started 2. Simple HTML Page with PHP The following is a basic example to output text using PHP. Copy the code onto your web server and save it as hello.php. Notice that the semicolon is used at the end of each line of PHP code to signify a line break. Like HTML, PHP ignores whitespace between lines of code. (An HTML equivalent is ) Semicolons are must!
  • Slide 14
  • Variables All variables in PHP start with a $ sign symbol. $var_name = value;
  • Slide 15
  • PHP: Loosely Typed A variable does not need to be declared before adding a value to it. Do not have to tell PHP which data type the variable is PHP automatically converts the variable to the correct data type, depending on its value. Compared to a strongly typed programming language..
  • Slide 16
  • Naming Rules for Variables A variable name must start with a letter or an underscore "_" A variable name can only contain alpha-numeric characters and underscores (a-z, A-Z, 0-9, and _ ) A variable name should not contain spaces. If a variable name is more than one word, it should be separated with an underscore ($my_string), or with capitalization ($myString)
  • Slide 17
  • PHP String Variables String variables are used for values that contains characters. A string can be used directly in a function or it can be stored in a variable. Below, the PHP script assigns the text "Hello World" to a string variable called $txt:
  • Slide 18
  • PHP String Variables The Concatenation Operator There is only one string operator in PHP. The concatenation operator (.) is used to put two string values together. To concatenate two string variables together, use the concatenation operator: The output: Hello World! What a nice day!
  • Slide 19
  • PHP String Variables The strlen() function is used to return the length of a string. The output: 12 The length of a string is often used in loops or other functions, when it is important to know when the string ends. (i.e. in a loop, we would want to stop the loop after the last character in the string).
  • Slide 20
  • PHP String Variables The strpos() function is used to search for character within a string. If a match is found, this function will return the position of the first match. If no match is found, it will return FALSE. The output: 6 The position of the string "world" in our string is position 6. The reason that it is 6 (and not 7), is that the first position in the string is 0, and not 1
  • Slide 21
  • PHP Operators Arithmetic Operators
  • Slide 22
  • PHP Operators Assignment Operators
  • Slide 23
  • PHP Operators Comparison Operators
  • Slide 24
  • PHP Operators Logical Operators
  • Slide 25
  • Conditional Statements Conditional statements are very useful for displaying specific content to the user. The if Statement Use the if statement to execute some code only if a specified condition is true. if (condition) code to be executed if condition is true;
  • Slide 26 ">
  • Conditional Statements The if...else Statement if (condition) code to be executed if condition is true; else code to be executed if condition is false; "; echo "Have a nice weekend!"; echo "See you on Monday!"; } ?>
  • Slide 27
  • Slide 28
  • Switch Statement switch (n) { case label1: code to be executed if n=label1; break; case label2: code to be executed if n=label2; break; default: code to be executed if n is different from both label1 and label2; } Dont forget break!
  • Slide 29
  • Slide 30
  • Array An array is a special variable, which can hold more than one value, at a time. $cars1="Saab"; $cars2="Volvo"; $cars3="BMW"; An array can hold all your variable values under a single name Three kinds of arrays: Numeric array - An array with a numeric index Associative array - An array where each ID key is associated with a value Multidimensional array - An array containing one or more arrays
  • Slide 31
  • Numeric Arrays Numeric index $cars=array("Saab","Volvo","BMW","Toyota"); $cars[0]="Saab"; $cars[1]="Volvo"; $cars[2]="BMW"; $cars[3]="Toyota"; echo $cars[0]. " and ". $cars[1]. " are Swedish cars."
  • Slide 32 32, "Quagmire"=>30, "Joe"=>34);">
  • Associative Arrays When storing data about specific named values, a numerical array is not always the best way to do it. Each ID key is associated with a value. We can use the values as keys and assign values to them. $ages = array("Peter"=>32, "Quagmire"=>30, "Joe"=>34);
  • Slide 33
  • A different way
  • Slide 34 array ( "Peter", "Lois", "Megan" ), "Quagmire"=>array ( "Glenn" ), "Brown"=>array ( "Cleveland", "Loretta", "Junior" ) );">
  • Multidimensional Arrays Each element in the main array can also be an array. Each element in the sub-array can be an array, and so on. $families = array ( "Griffin"=>array ( "Peter", "Lois", "Megan" ), "Quagmire"=>array ( "Glenn" ), "Brown"=>array ( "Cleveland", "Loretta", "Junior" ) );
  • Slide 35
  • echo "Is ". $families['Griffin'][2]. " a part of the Griffin family?"; The code above will output: Is Megan a part of the Griffin family?
  • Slide 36
  • Looping The same block of code to run over and over again Looping statements: while - loops through a block of code while a specified condition is true do...while - loops through a block of code once, and then repeats the loop as long as a specified condition is true for - loops through a block of code a specified number of times foreach - loops through a block of code for each element in an array
  • Slide 37 "> "> " title="while "; $i++; } ?>">
  • while "; $i++; } ?>
  • Slide 38
  • do...while "; } while ($i
  • Slide 39
  • for for (init; condition; increment) { code to be executed; } Parameters: init: Mostly used to set a counter (but can be any code to be executed once at the beginning of the loop) condition: Evaluated for each loop iteration. If it evaluates to TRUE, the loop continues. If it evaluates to FALSE, the loop ends. increment: Mostly used to increment a counter (but can be any code to be executed at the end of the loop)
  • Slide 40 "> "> " title="for "; } ?>">
  • for "; } ?>
  • Slide 41 "> "> " title="foreach "; } ?>">
  • foreach "; } ?>
  • Slide 42
  • Activity Implement the following table in a multi-dimensional array "student: Output the student table in form of a table. NameIDGrade Tom000198 John000581 Jane000670
  • Slide 43