php app development 1

2,690 views

Published on

Slides used from an introduction to PHP

Published in: Education
0 Comments
0 Likes
Statistics
Notes
  • Be the first to comment

  • Be the first to like this

No Downloads
Views
Total views
2,690
On SlideShare
0
From Embeds
0
Number of Embeds
926
Actions
Shares
0
Downloads
1
Comments
0
Likes
0
Embeds 0
No embeds

No notes for slide
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • php app development 1

    1. 1. Web Scripting using PHP
    2. 2. Web Scripting using PHP Server side scripting
    3. 3. So what is a Server Side Scripting Language?• Programming language code embedded into a web page PERL PHP PYTHON ASP
    4. 4. So what is a Server Side Scripting Language?• Programming language code embedded into a web page PERL PHP PYTHON ASP
    5. 5. Different ways of scripting the Web• Programming language code embedded into a web page No scripting (plain markup) Client Side scripting Ser ver Side scripting Combination of the above (AJAX)
    6. 6. No Scripting example - how it works...User on a machine somewhere Ser ver machine
    7. 7. Being more specific...
    8. 8. Web Browser soft ware
    9. 9. Web ser versoft ware
    10. 10. User types in a URL for a page with noprogramming code inside
    11. 11. User types in a URL for a page with noprogramming code insideUniform Resource Locator
    12. 12. Request is sent toser ver using HTTP
    13. 13. Request is sent to ser ver using HTTPHypertext Transfer Protocol
    14. 14. Ser ver soft warefinds the page
    15. 15. Page is sent back,using HTTP
    16. 16. Browser renders /displays the page
    17. 17. Server Side scripting
    18. 18. User types in a URL for a page withPHP code inside
    19. 19. Request is sent toser ver using HTTP
    20. 20. Ser ver soft warefinds the page
    21. 21. Ser ver side codeis executed
    22. 22. Page is sent back,using HTTP
    23. 23. Browser renders /displays the page
    24. 24. Server side scripting languages • Executes in the ser ver • Before the page is sent from server to browser • Ser ver side code is not visible in the client • Ser ver side code can access resources on the server side
    25. 25. Browser Web ser ver Database ser ver
    26. 26. How many items in stock?
    27. 27. HTTP request
    28. 28. Web ser verexecutes code
    29. 29. Web ser ver executes codeQueriesdatabaseser ver
    30. 30. Resultsentback
    31. 31. HTMLgenerated
    32. 32. HTTP response
    33. 33. Answer displayed
    34. 34. So why PHP? PERL PHP PYTHON ASP
    35. 35. PHP usage ... • Source: PHP programming 2nd Ed.
    36. 36. PHP compared to others ... • Source: www.securityspace.com
    37. 37. Books - core syntaxProgramming PHP, Second Edition PHP in a NutshellBy Kevin Tatroe, Rasmus Lerdorf, By Paul HudsonPeter MacIntyre First Edition October 2005Second Edition April 2006** Recommended
    38. 38. Books - learning / tutorial basedLearning PHP 5 Learning PHP and MySQLBy David Sklar By Michele Davis, Jon PhillipsFirst Edition June 2004 First Edition June 2006
    39. 39. Other texts.. • There are other publishers / texts (trade books) • Look for books that cover PHP 5 • Open source, server side languages can rapidly develop • Features added or deprecated rapidly
    40. 40. PHP development PHP 5 PHP 4 PHP 3 PHP 1 • 5 versions in 10 years
    41. 41. Language basics
    42. 42. Language basics • Embedding PHP in Web pages • Whitespace and Line breaks • Statements and semicolons • Comments • Literals / names • Identifiers • Keywords • Data types
    43. 43. Language basics • Embedding PHP in Web pages • Whitespace and Line breaks • Statements and semicolons • Comments • Literals / names • Identifiers • Keywords • Data types Much of this material is explained in PHP programming 2nd Ed. Chap 1 & 2
    44. 44. Embedding PHP in web pages Use <?php and ?> to <?php surround the php code statement; statement; statement ?>
    45. 45. Embedding PHP in web pages <?php statement;statement; statement; statement; statement;statement; ?> In general whitespace doesn’t matter Use indenting and separate lines to create readable code
    46. 46. The legendary Hello World program <!DOCTYPE HTML PUBLIC "-//W3C/DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> <html> <head> <title>This is the first PHP program</title> </head> <body> <p> <?php print "Hello World!"; ?> </p> </body> </html>
    47. 47. The legendary Hello World program <!DOCTYPE HTML PUBLIC "-//W3C/DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> <html> <head> <title>This is the first PHP program</title> </head> <body> <p> <?php print "Hello World!"; ?> </p> </body> </html>
    48. 48. The legendary Hello World program <!DOCTYPE HTML PUBLIC "-//W3C/DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> <html> <head> <title>This is the first PHP program</title> </head> <body> <p> print sends a sequence of <?php print "Hello World!"; characters to the output ?> </p> The sequence here is </body> indicated by start and </html> end quotes
    49. 49. PHP can be put ‘ nywhere’.. a All the php blocks are processed <html> <?php ... ?> before the page is sent <head> <?php ... ?> <title>... <?php ... ?> ...</title> </head> <body> <p> <?php ... ?> </p> </body> </html>
    50. 50. PHP can be put ‘ nywhere’.. but works in sequence a <html> <?php ... ?> Starting at the top <head> <?php ... ?> <title>... <?php ... ?> ...</title> </head> <body> <p> <?php ... ?> Working down to the bottom </p> </body> </html>
    51. 51. Statements and semicolons Use ; to separate <?php statements statement; statement; statement; ?>
    52. 52. All of these would work the same way...<?php statement; statement;statement ?><?phpstatement; statement;statement;?><?phpstatement;statement; This is the best way ofstatement; laying the code out?>
    53. 53. CommentsMany different ways to add commentsComment Source Action // C++ Comments to EOL # Unix shell scripting Comments to EOL /* and */ C Comments out a block
    54. 54. Comments <?php php statement; // A comment here php statement; # Another comment here /* A series of lines with comments ignored by the PHP processor */ php statement; ?>
    55. 55. Comments <?php php statement; // A comment here php statement; # Another comment here /* A series of lines with comments ignored by the PHP processor */ php statement; ?>Everything in red is ignored by the PHP interpreter
    56. 56. Language basics • Embedding PHP in Web pages • Whitespace and Line breaks • Statements and semicolons • Comments • Literals / names • Identifiers • Keywords • Data types
    57. 57. Language basics • Embedding PHP in Web pages ✔ • Whitespace and Line breaks ✔ • Statements and semicolons ✔ • Comments ✔ • Literals / names • Identifiers • Keywords • Data types
    58. 58. LiteralsA data value that appears directly in the program 2001 An integer 0xFE Hexadecimal number 1.4142 Float “Hello World” String ‘Hi’ String true Bool null built in ‘no value’ symbol
    59. 59. IdentifiersIdentifiers (or names) in PHP must - Begin with an ASCII letter (uppercase or lowercase) or begin with the underscore character _ or any character bet ween ASCII 0x7F to 0xFF followed by any of these characters and the digits 0-9
    60. 60. VariablesVariables in PHP are identifiers prefixed by $ $bill $value_count $anothervalue3 Valid $THIS_IS_NOT_A_GOOD_IDEA $_underscore $not valid Invalid $[ $3wa
    61. 61. Case sensitivity - names we define are case sensitive $value Three different names $VALUE $vaLUE
    62. 62. VariablesWe use variables for items of data that will changeas the program runsChoose a sensible name and have as many as youlike $total_income $bill $salary $month $total $percentage_increase
    63. 63. VariablesWhen we declare a variable, a space is reservedand labelled for that item (in memory) $bill $bill
    64. 64. VariablesTo give it a value, use the equals sign $bill $bill
    65. 65. VariablesTo give it a value, use the equals sign $bill = 42 $bill
    66. 66. VariablesTo give it a value, use the equals sign $bill = 42 42 $bill
    67. 67. VariablesTo give it a value, use the equals sign $bill $bill
    68. 68. VariablesTo give it a value, use the equals sign $bill = 57.98 $bill
    69. 69. VariablesTo give it a value, use the equals sign $bill = 57.98 57.98 $bill
    70. 70. VariablesTo give it a value, use the equals sign $bill $bill
    71. 71. VariablesTo give it a value, use the equals sign $bill = “No payment” $bill
    72. 72. VariablesTo give it a value, use the equals sign $bill = “No payment” “No payment” $bill
    73. 73. VariablesIf a value is changed, the old value is over written $bill $bill
    74. 74. VariablesIf a value is changed, the old value is over written $bill = 42; $bill
    75. 75. VariablesIf a value is changed, the old value is over written $bill = 42; 42 $bill
    76. 76. VariablesIf a value is changed, the old value is over written $bill = 42; 42 $bill = 58; $bill
    77. 77. VariablesIf a value is changed, the old value is over written $bill = 42; 58 $bill = 58; $bill
    78. 78. VariablesSometimes we use the old value to recalculate thenew value $bill $bill
    79. 79. VariablesSometimes we use the old value to recalculate thenew value $bill = 42; $bill
    80. 80. VariablesSometimes we use the old value to recalculate thenew value $bill = 42; 42 $bill
    81. 81. VariablesSometimes we use the old value to recalculate thenew value $bill = 42; 42 $bill = $bill*2 ; $bill
    82. 82. VariablesSometimes we use the old value to recalculate thenew value $bill = 42; 84 $bill = $bill*2 ; $bill
    83. 83. VariablesSome languages are very strict about what kinds ofdata are stored in variables - PHP doesn’t care $bill=42; Stores an integer $bill=42; $bill=”Now its a string”; print $bull;
    84. 84. VariablesSome languages are very strict about what kinds ofdata are stored in variables - PHP doesn’t care $bill=42; Stores an integer $bill=42; Over writes with a string $bill=”Now its a string”; print $bull;
    85. 85. VariablesSome languages are very strict about what kinds ofdata are stored in variables - PHP doesn’t care $bill=42; Stores an integer $bill=42; Over writes with a string $bill=”Now its a string”; Whoops - made a mistake print $bull; but it still works
    86. 86. VariablesSome languages are very strict about what kinds ofdata are stored in variables - PHP doesn’t care $bill=42; Stores an integer $bill=42; Over writes with a string $bill=”Now its a string”; Whoops - made a mistake print $bull; but it still works
    87. 87. Keywords _CLASS_ _ Declare extends print( ) _ _FILE_ _ Default final privateReserved by _ _FUNCTION_ _ die( ) for protectedthe language _ _LINE_ _ Do foreach publicfor core _ _METHOD_ _ echo( ) function require( ) Else globalfunctionality Abstract And elseif if require_once( ) array( ) empty( ) implements return( ) As enddeclare include( ) static Break endfor include_once( ) switchAlso - can’t Case catch endforeach interface tHRow endif isset( )use a built in cfunction endswitch list( ) TRy unset( )function Class endwhile new use clonename as a Const eval( ) old_function var exception Orvariable Continue exit( ) php_user_filter while xor
    88. 88. Data typesPHP provides 8 types scalar (single-value) compound integers arrays floating-point objects string booleans Two are special - resource and NULL
    89. 89. IntegersWhole numbers - range depends on the C compilerthat PHP was made in (compiled in)Typically +2,147,483,647 to -2,147,483,647 Octal 0755 Hexadecimal 0xFFLarger integers get converted to floats automatically
    90. 90. Floating-Point NumbersReal numbers - again range is implementation specificTypically 1.7E-308 to 1.7E+308 with 15 digits of accuracyExamples 3.14, 0.017, -7.1, 0.314E1, 17.0E-3
    91. 91. StringsDelimited by either single or double quotes ‘here is a string’ “here is another string”
    92. 92. Strings - single quotesYou can use single quotes to enclose double quotes $outputstring=‘He then said “Goodbye” and left’;Useful for easily printing HTML attributes $outputstring=‘<a href=”http:/www.bbc.co.uk”>BBC</a>’;
    93. 93. Strings - double quotesYou can use double quotes to enclose single quotes $outputstring=”He then said ‘Goodbye’ and left”;
    94. 94. OperatorsStandard arithmetic operators: +, -, *, /, % ..Concatenation operator: . $outputstring=”He then said “.$quote;Any non-string value is converted to a string beforethe concatenation.
    95. 95. Operators$aBool=true;$anInt=156;$aFloat=12.56;$anotherFloat=12.2E6;$massiveFloat=12.2E-78;print "The bool printed looks like this: ".$aBool."<br />";print "The int printed looks like this: ".$anInt."<br />";print "The (smaller) float printed looks like this: ".$aFloat."<br />";print "The larger float printed looks like this: ".$anotherFloat."<br />";print "The even larger float printed looks like this: ".$massiveFloat."<br />";
    96. 96. Operators$aBool=true;$anInt=156;$aFloat=12.56;$anotherFloat=12.2E6;$massiveFloat=12.2E-78;print "The bool printed looks like this: ".$aBool."<br />";print "The int printed looks like this: ".$anInt."<br />";print "The (smaller) float printed looks like this: ".$aFloat."<br />";print "The larger float printed looks like this: ".$anotherFloat."<br />";print "The even larger float printed looks like this: ".$massiveFloat."<br />";

    ×