1 / 29

IT420: Database Management and Organization

Learn about PHP, a powerful server-side scripting language for managing and organizing databases. Discover its strengths, syntax, variables, constants, control statements, and more.

lupet
Download Presentation

IT420: Database Management and Organization

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. IT420: Database Management and Organization PHP 28 February 2006 Adina Crainiceanu www.cs.usna.edu/~adina

  2. Web Applications • Choose: • Operating system – Windows or Linux • Web server software – Apache • Database Management System – MySQL • Programming or scripting language – PHP

  3. 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

  4. 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

  5. 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

  6. 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

  7. 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

  8. Sokkit demo • Save .php files in C:\Sokkit\site • Run http://localhost/yourphpfile.php

  9. 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

  10. PHP Tags • Preferred style: • <?php echo “Hello World”; ?> • Other styles: • <? echo “Hello World”; ?> • <SCRIPT LANGUAGE=‘php’> echo “Hello World”; </SCRIPT> • <% echo “Hello World”; %>

  11. PHP Statements • Tell PHP interpreter what to do • Example: echo, if, while … <?php echo “Hello World";?> • Separated by semicolon

  12. 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

  13. 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

  14. 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

  15. PHP Constants • define(‘CONST1’, 5) • $test2 = CONST1 * $test1; Constant Name – No $

  16. 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

  17. 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

  18. Arithmetic Operators

  19. Assignment Operators

  20. Comparison Operators

  21. Logical Operators

  22. Control Statements • If (condition) {do something} else { do something else} • Switch(variable) { case value1: someCode2; break; case value2: someCode2; break; … default: someCode; break; }

  23. Control Statements (cont) • while(condition){ do something} • do {something} while (condition) • for(init; condition; increment){something} • foreach(array as value){something} Used for arrays

  24. Arrays • $numbers = array(‘one’, ‘two’, ‘three’); • $arr[0] == ‘one’ is true • foreach($numbers as $current){ echo “Current element is $current”; }

  25. 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).

  26. 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.

  27. 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>

  28. 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

  29. 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!

More Related