1 / 62

SCV1223

SCV1223. PHP - Hypertext Preprocessor. Introduction. PHP is a powerful server-side scripting language for creating dynamic and interactive websites. PHP is the widely-used, free, and efficient alternative to competitors such as Microsoft's ASP.

moira
Download Presentation

SCV1223

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. SCV1223 PHP - Hypertext Preprocessor

  2. Introduction • PHP is a powerful server-side scripting language for creating dynamic and interactive websites. • PHP is the widely-used, free, and efficient alternative to competitors such as Microsoft's ASP. • PHP is perfectly suited for Web development and can be embedded directly into the HTML code. • The PHP syntax is very similar to Perl and C. • PHP is often used together with Apache (web server) on various operating systems.

  3. What is PHP? • PHP stands for PHP: Hypertext Preprocessor • PHP is a server-side scripting language, like ASP • PHP scripts are executed on the server even though even though combined with an HTML code • PHP supports many databases (MySQL, Informix, Oracle, Sybase, Solid, PostgreSQL, Generic ODBC, etc.) • PHP is an open source software (OSS) • PHP is free to download and use

  4. What is a PHP File? • PHP files may contain text, HTML tags and scripts • PHP files are returned to the browser as plain HTML  • PHP files have a file extension of ".php", ".php3", or ".phtml"

  5. What is MySQL? • MySQL is a database server • MySQL is ideal for both small and large applications • MySQL supports standard SQL • MySQL compiles on a number of platforms • MySQL is free to download and use

  6. PHP + MySQL • PHP combined with MySQL are cross-platform (means that you can develop in Windows and serve on a Unix platform)

  7. Why PHP? • PHP runs on different platforms (Windows, Linux, Unix, etc.) • PHP is compatible with almost all servers used today (Apache, IIS, etc.) • PHP is FREE to download from the official PHP resource: www.php.net • PHP is easy to learn and runs efficiently on the server side

  8. Where to Start? • Install an Apache server on a Windows or Linux machine • Install PHP on a Windows or Linux machine • Install MySQL on a Windows or Linux machine • Install PHP triad • All in one • LAMP • Linux Apache MySQL PHP

  9. First Example – Hello World! • Activity 01 • NOTES • You cannot view the PHP source code by selecting "View source" in the browser • you will only see the output from the PHP file, which is plain HTML. • This is because the scripts are executed on the server before the result is sent back to the browser.

  10. PHP Syntax • A PHP scripting block always starts with <?php and ends with ?>. • A PHP scripting block can be placed anywhere in the HTML document. • On servers with shorthand support enabled you can start a scripting block with <? and end with ?>. • However, for maximum compatibility, it is recommend that you use the standard form (<?php) rather than the shorthand form.

  11. PHP Syntax • A PHP file normally contains HTML tags, just like an HTML file, and some PHP scripting code. • View source for activity 01 • Each code line in PHP must end with a semicolon. • The semicolon is a separator and is used to distinguish one set of instructions from another. • There are two basic statements to output text with PHP: echo and print. • In activity 01, we have used the echo statement to output the text "Hello World".

  12. PHP Syntax • Comments in PHP • In PHP, we use // to make a single-line comment or • /* and */ to make a large comment block.

  13. PHP Variables • All variables in PHP start with a $ sign symbol. • Variables may contain strings, numbers, or arrays (untype). • Activity 02 • Variable concatenation – using the (.) operator • All converted to string • Str . Str = str • Num . Num = str

  14. Variable Naming Rules • Same as C, C++ or Java • A variable name must start with a letter or an underscore "_" • A variable name can only contain alpha-numeric characters and underscores (a-Z, 0-9, and _ ) • A variable name should not contain spaces. • If a variable name should be more than one word, it should be separated with underscore ($my_string), • or with capitalization ($myString)

  15. HTML out HelloWorld <IMG> tag <A> tag <TABLE> tag

  16. PHP operator, conditional • Same as C or C++ • PHP If...Else Statements – Activity 03 • Same as C or C++ • Swith statement • Same as C or C++

  17. PHP array • Numeric Arrays (Activity 04) • $names = array("Peter","Quagmire","Joe"); • Assign manualy: • $names[0] = "Peter"; • $names[1] = "Quagmire"; • $names[2] = "Joe"; • Assign directly: • $family = array('Fred', 'Wilma');

  18. PHP array • Adding Values to the End of an Array • $family[] = 'Pebbles'; // $family[2] is 'Pebbles' • Getting the Size of an Array • $size = count($family); // $size is 3

  19. PHP array • Traversing Arrays • $family = array('Fred', 'Wilma'); • foreach( $family as $name) {} • for($i = 0; $i < count($family); $i++) {}

  20. PHP Looping – Activity 05 • Same as C or C++ • while - loops through a block of code if and as long as a specified condition is true • do...while - loops through a block of code once, and then repeats the loop as long as a special 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

  21. PHP object

  22. PHP object

  23. PHP object

  24. PHP object • Activity 7 • Array of objects • Table tag – array of objects

  25. PHP Function • Also similar to C, C++ and Java • Normal – 8-1 • With parameter – 8-2 • Return value – 8-3 • Pass by value • Pass by reference

  26. PHP – Data transfer • Transferring data between client (web browser) to the server-side • Two method: • Using form: POST method • Through URL: GET method

  27. POST Method • The body of the message is sent as a stream of data (HTML form data) • Separated with the PHP URL in the FORM post URL • Client send data to PHP using HTML form element

  28. POST Method • Form tag <FORM METHOD=”post” ACTION=”login.php” TARGET=“”> • Fill the TARGET value if form result have to display in a different frame • After coding all the form element (button, textfield, etc) FORM tag must be close using the equivalent end tag - </FORM> • If you have multiple form in a single page every separate every form using the end tag

  29. The Form (9.2.1) • Text (textfield, password and textarea) • Single data entry • <input name="name" type="text" id="name" /> • <input type="password" name="password" id="password" /> • <textarea name="address" id="address" cols="30" rows="5"></textarea>

  30. The Form (9.2.1) • Radiobutton • Single data entry • <input name="gender" type="radio" value="male" /> • <input name="gender" type="radio" value="female" />

  31. The Form (9.2.1) • Checkbox • Multiple data entry • <input name="cc[]" type="checkbox" id="cc[]" value="MC" /> • <input name="cc[]" type="checkbox" id="cc[]" value="VISA" /> • <input name="cc[]" type="checkbox" id="cc[]" value="AMEX" />

  32. The Form (9.2.1) • Combobox • Single data entry • <select name="bank" id="bank"> • <option value=“MBB”>Maybank</option> • <option>AMBANK</option> • <option>CIMB</option> • <option>HSBC</option> • </select>

  33. The Form (9.2.1) • Selectbox • Multiple data entry • <select name="books[]" size="5" multiple="multiple" id="books"> • <option value="hp1">Harry Potter 1</option> • <option value="hp2">Harry Potter 2</option> • <option value="hp3">Harry Potter 3</option> • <option value="hp4">Harry Potter 4</option> • <option value="hp5">Harry Potter 5</option> • </select>

  34. GET method (Activity 10) • The body of the message (the data) is appended to the PHP URL, • http://myserver.com/hello.php • Separated by a question mark • http://myserver.com/hello.php? • Followed by name-value pair which separated by equals sign • If value consist of more than one word, separate it using plus sign which the php will convert it to space character after parsing • name=john+doe • Every consecutive name-value pair will be separated using ampersand sign (&) • name=john+doe&id=007

  35. Database (MySQL) Activity 11

  36. PHP MySQL – Connect/Disconnect

  37. PHP MySQL – mysql_query() • mysql_query ( string $query [, resource $link_identifier] ) • $query • A SQL query • The query string should not end with a semicolon. • $link_identifier • DB connection • If is not specified, the last link opened by mysql_connect() is assumed otherwise DB error

  38. PHP MySQL – Create (Db & Table)

  39. PHP MySQL – Create (Db & Table)

  40. PHP MySQL – Create (Db & Table)

  41. PHP MySQL – Create (Db & Table)

  42. PHP MySQL – Create (Db & Table)

  43. PHP MySQL – Create (Db & Table)

  44. PHP MySQL – Create (Db & Table)

  45. PHP MySQL – INSERT

  46. PHP MySQL – INSERT

  47. PHP MySQL – SELECT

  48. PHP MySQL – SELECT

  49. PHP MySQL – SELECT • The example above stores the data returned by the mysql_query() function in the $result variable. • Next, we use the mysql_fetch_array() function to return the first row from the recordset as an array. • Each subsequent call to mysql_fetch_array() returns the next row in the recordset. • The while loop loops through all the records in the recordset. • To print the value of each row, we use the PHP $row variable ($row['FirstName'] and $row['LastName']).

More Related