170 likes | 192 Views
Web Site Design and Authoring. Session 13 Scott Marino. Topics. Introduction to PHP PHP Syntax Forms and Form Elements Numbers and Strings Conditionals and Loops Arrays and Regular Expressions. Introduction to PHP. Originally called Personal Home Page (PHP)
E N D
Web Site Design and Authoring Session 13 Scott Marino Scott Marino MSMIS Summer Session 1 - 2001
Topics • Introduction to PHP • PHP Syntax • Forms and Form Elements • Numbers and Strings • Conditionals and Loops • Arrays and Regular Expressions Scott Marino MSMIS Summer Session 1 - 2001
Introduction to PHP • Originally called Personal Home Page (PHP) • Now called Hypertext Preprocessor • Created in 1994 as an open source project • Server-Side execution • Cross Platform compatible • Unix, Windows, Mac, OS/2 • HTML embedded Scott Marino MSMIS Summer Session 1 - 2001
Why use PHP • Better, faster, and easier to learn than CGI, ASP, and JSP • PHP does much more than you can do with basic HTML • PHP was written specifically to create web pages • Perl, VBScript and Java were adapted to work on the web • Can’t do everything that other programming languages can Scott Marino MSMIS Summer Session 1 - 2001
Basic Syntax • <PHP? … ?> • Directive to PHP to interpret the code between the tags and perform the instructions • <PHP? phpinfo(); ?> • The phpinfo function prints installation information about PHP • Every instruction ends with a semicolon • Files are saved with a .php extension instead of .htm for Apache Scott Marino MSMIS Summer Session 1 - 2001
Basic Syntax • print(“Hello there”); • print(“<b>Hello there</b>”); • Print function to embed text or html into the web page • // or # denotes a single line comment • /* (several text lines) */ for commenting multiple lines Scott Marino MSMIS Summer Session 1 - 2001
Basic Syntax • Variable Types • Number • String • Array • $num_var = 1; • $float_num_var = 1.2; • $string_var = “PHP is cool!”; • Look at phpinfo(); for available environment variables Scott Marino MSMIS Summer Session 1 - 2001
Forms and PHP • PHP can access data elements from a form • Instead of calling a cgi script in the form action, the action will point to a PHP page • Can use both post and get methods • <input type=text name=lname> (on the form) • print(“$lname”); • Prints the PHP variable from the form field name Scott Marino MSMIS Summer Session 1 - 2001
Formatting Numbers • To print a $ sign, you must “escape” it • print (“total dollars = \$$total_dollars”); • The printf(); function can format numbers for printing • Supports mathematical precedence with the use of parenthesis • Has built in functions like round(); and abs(); • Can create random numbers with srand(); and rand(); Scott Marino MSMIS Summer Session 1 - 2001
String Formatting • trim(); removes leading and trailing spaces from string fields • ltrim(); and rtrim(); functions are available • $string = $astring . $bstring; • $string = $astring . “ “ . $bstring; • urlencode(); is used to convert characters to the proper format for a url • urldecode(); is used to convert an encoded url back to standard format Scott Marino MSMIS Summer Session 1 - 2001
String Formatting • strtok(); Creates a string from a token • $f_name = strtok($fullname, “ “); • substr($string,0,10); • Parses the first 10 characters of the contents of $string starting with position 0 • strlen($string); • Counts the number of characters in $string Scott Marino MSMIS Summer Session 1 - 2001
Conditionals • if (condition) {true action;} else {false action;} • If the condition or test results in “true” then execute the “true action” else execute the “false action” Scott Marino MSMIS Summer Session 1 - 2001
Conditionals • if (condition) {true action;} elseif (condition) {true action;} else {false action;} • The elseif allows for additional levels of conditions to be checked Scott Marino MSMIS Summer Session 1 - 2001
Conditionals • switch ($state) { case "AL": print ("Alabama"); break;….. default: print(”State of Confusion"); • Used for a longer list of conditions than is easily handled with elseif Scott Marino MSMIS Summer Session 1 - 2001
Looping • while (condition) {statements;} • do {statements;} while (condition); • for ($I = 1; $I <= somecount; $I++) {statements;} Scott Marino MSMIS Summer Session 1 - 2001
Arrays • $list = array(“apples”,”peaches”,”pears”); • Creates a simple array with elements in array positions 0,1, and 2 • $list = array(1=>“apples”, 2=>”peaches”, 3=>”pears”); • Creates an indexed array with elements in array positions 1, 2, and 3 • $list[] = “grapes”; • Adds element “grapes” to the end of the array Scott Marino MSMIS Summer Session 1 - 2001
Regular Expressions • PHP Supports Regular Expressions • ereg(“pattern”,”string”); • ereg is a case sensitive match • $pattern = “pattern”;$string = “string”;eregi($pattern, $string); • eregi is a case insensitive match Scott Marino MSMIS Summer Session 1 - 2001