290 likes | 431 Views
IT420: Database Management and Organization. PHP 28 February 2006 Adina Crainiceanu www.cs.usna.edu/~adina. Web Applications. Choose: Operating system – Windows or Linux Web server software – Apache Database Management System – MySQL Programming or scripting language – PHP . PHP.
E N D
IT420: Database Management and Organization PHP 28 February 2006 Adina Crainiceanu www.cs.usna.edu/~adina
Web Applications • Choose: • Operating system – Windows or Linux • Web server software – Apache • Database Management System – MySQL • Programming or scripting language – PHP
PHP • PHP: PHP Hypertext Preprocessor • Server-side scripting language • Browser never sees PHP - only the web server sees it • PHP pages require a web server with PHP support • Competitors: Perl, ASP.NET, JSP
PHP Strengths • High performance • Interface to many different database systems • Built-in libraries • Ease of learning and use • Object-oriented support • Portability • Open source • Free • Availability of support
PHP References • Online references • http://www.php.net • Online Tutorial • http://www.w3schools.com/php/default.asp • PHP and MySQL Web Development by Luke Welling and Laura Thomson • MySQL/PHP Database Applications by Brad Bulger, Jay Greenspan, David Wall
Example PHP Page – Server Side <h2>Today's Headline:</h2><p><?php echo "World Peace Declared";?></p> Code as it appears on the Server side Begin and end PHP code to be interpreted by server PHP engine
Example PHP Page Processed – Client Side <h2>Today's Headline:</h2><p> "World Peace Declared“ </p> Code as it appears on the client browser side
Sokkit demo • Save .php files in C:\Sokkit\site • Run http://localhost/yourphpfile.php
Basic PHP Syntax • <html> • <body> • <?php echo "Hello World"; # this is a PHP test program ?> • </body> • </html> • HTML code • PHP tags • PHP statements • White space • Comments
PHP Tags • Preferred style: • <?php echo “Hello World”; ?> • Other styles: • <? echo “Hello World”; ?> • <SCRIPT LANGUAGE=‘php’> echo “Hello World”; </SCRIPT> • <% echo “Hello World”; %>
PHP Statements • Tell PHP interpreter what to do • Example: echo, if, while … <?php echo “Hello World";?> • Separated by semicolon
White space And Comments • White space is ignored • Comments: • /* multi-line comment here */ • // comment here • # comment here # here is a comment ?> here is no comment
PHP Variables • Variable names start with $ • $testvariable = 0; • Case sensitive • Variables do not have to be declared • Created when a value is assigned • Data types: • Integer • Float • String • Boolean • Array • Object
PHP Variables (cont.) • Variables type determined by content • $test1 = 0; //integer • $test2 = 0.0; //float • $test2 = $test1; //integer • $test2 = “Hello”; //Is this possible? • Variable variables • $varname = ‘test1’; • $$varname = 5; //result: $test1 = 5
PHP Constants • define(‘CONST1’, 5) • $test2 = CONST1 * $test1; Constant Name – No $
Strings • String concatenation operator: . (dot) • $test1 = ‘I’m’; • Echo $test1.’ John’ • Single quotes strings: • True literals • Double quotes strings – variables are evaluated (interpolation): • Echo “$test1 John” Concatenate
Lab Exercise • Start Sokkit • Create and run helloworld.php • Modify helloworld.php to use variables • Assign some text to two variables • Print the text obtained by concatenation
Control Statements • If (condition) {do something} else { do something else} • Switch(variable) { case value1: someCode2; break; case value2: someCode2; break; … default: someCode; break; }
Control Statements (cont) • while(condition){ do something} • do {something} while (condition) • for(init; condition; increment){something} • foreach(array as value){something} Used for arrays
Arrays • $numbers = array(‘one’, ‘two’, ‘three’); • $arr[0] == ‘one’ is true • foreach($numbers as $current){ echo “Current element is $current”; }
Lab Exercise • Create an array containing the following values: (1900, 2000, 2004, 2005 ). • Use the array in a “foreach” loop to test if the value is a leap year. • If it is a leap year print “XXXX is a leap year” • Else print “XXXX is not a leap year” • A year is a leap year if it is divisible by 4. If the year is also divisible by 100 then it is NOT a leap year unless it also divisible by 400. Thus, 1900 is not a leap year (divisible by 100 but not 400) while 2000 is a leap year (divisible by 400).
Form Processing Web Server Browser User enters information into the form then “Submits” the form User sees the results. • Server receives form. • The form’s action is the name of the PHP script • PHP engine parser decodes the form contents and variables then processes the script • Results sent back to the browser. Connection closes.
Input Form <html> <body> <form action=“name_age.php" method="POST"> Enter your name: <input type="text" name="name" /> Enter your age: <input type="text" name="age" /> <input type="submit" /> </form> </body> </html>
Accessing the Form Variables • name_age.php needs to know the values of the input fields • $age //short style • Requires ‘register_globals’ • $_POST[‘age’] //medium style • $_GET[‘age’] if method is GET • $_REQUEST[‘age’] • $HTTP_POST_VARS[‘age’] //long style
name_age.php – Called on Submit <html> <body> Welcome <?php echo $_POST[‘name’]; ?>.<br /> You are <?php echo $_POST[‘age’]; ?> years old! </body> </html> Sample output: Welcome John. You are 28 years old!