PHP for NonProgrammers (DrupalCon SF 2010)

4,735 views

Published on

Session offered at DrupalCon San Francisco 2010. "Physics for Poets" introduction to PHP concepts like variables, arrays, conditionals, and functions.

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

No Downloads
Views
Total views
4,735
On SlideShare
0
From Embeds
0
Number of Embeds
13
Actions
Shares
0
Downloads
0
Comments
0
Likes
4
Embeds 0
No embeds

No notes for slide
































































































  • PHP for NonProgrammers (DrupalCon SF 2010)

    1. 1. Today’s Tools Write code: http://www.codepad.com Select PHP as the language! Ask questions: IRC: irc.freenode.net #php4np Twitter: php4np Robert Ristroph is here for you!
    2. 2. PHP For NonProgrammers Programmer: One who writes computer programs. NonProgrammer: One who will write PHP code today while learning Diana’s Five Rules of Programming and earning geek points. DrupalCon San Francisco 2010
    3. 3. You Win! Score more than 5 points during today’s presentation and win the Grand Prize Geek Award* (GPGA). * Four Kitchens’ stickers and the right to change your Facebook language to English(Pirate).
    4. 4. <?php /** * About Me */ // also comments $name = “Diana Montalion Dupuis”; $fourKitchensWebChef = TRUE; $numPets = 3; $currBooks = array(“The World Without Us”, “Outliers”, “Brain Rules”, “Wolf Hall”); if ($notWorking == TRUE) { echo “I’m reading a book or watching football.”; } else { echo “I’m writing PHP code.”; } ?> Write your own version!
    5. 5. <?php /** comments * About Me */ // also comments $name = “Diana Montalion Dupuis”; $fourKitchensWebChef = TRUE; $numPets = 3; $currBooks = array(“The World Without Us”, “Outliers”, “Brain Rules”, “Wolf Hall”); if ($notWorking == TRUE) { echo “I’m reading a book or watching football.”; } else { echo “I’m writing PHP code.”; } ?> Write your own version!
    6. 6. <?php /** comments * About Me */ // also comments $name = “Diana Montalion Dupuis”; $fourKitchensWebChef = TRUE; variables $numPets = 3; $currBooks = array(“The World Without Us”, “Outliers”, “Brain Rules”, “Wolf Hall”); if ($notWorking == TRUE) { echo “I’m reading a book or watching football.”; } else { echo “I’m writing PHP code.”; } ?> Write your own version!
    7. 7. <?php /** comments * About Me */ // also comments $name = “Diana Montalion Dupuis”; $fourKitchensWebChef = TRUE; variables $numPets = 3; $currBooks = array(“The World Without Us”, “Outliers”, “Brain Rules”, “Wolf Hall”); if ($notWorking == TRUE) { array echo “I’m reading a book or watching football.”; } else { echo “I’m writing PHP code.”; } ?> Write your own version!
    8. 8. <?php /** comments * About Me */ // also comments $name = “Diana Montalion Dupuis”; $fourKitchensWebChef = TRUE; variables $numPets = 3; $currBooks = array(“The World Without Us”, “Outliers”, “Brain Rules”, “Wolf Hall”); if ($notWorking == TRUE) { array echo “I’m reading a book or watching football.”; } else { echo “I’m writing PHP code.”; conditional } ?> Write your own version!
    9. 9. What does PHP mean? http://en.wikipedia.org/wiki/PHP
    10. 10. What does PHP mean? Who cares?!? http://en.wikipedia.org/wiki/PHP
    11. 11. What does PHP mean? Who cares?!? The question and answer that matter most are the http://en.wikipedia.org/wiki/PHP
    12. 12. What does PHP mean? Who cares?!? The question and answer that matter most are the PHP: Hypertext Preprocessor is a widely used, general-purpose scripting language that was originally designed for web development to produce dynamic web pages. For this purpose, PHP code is embedded into the HTML source document and interpreted by a web server with a PHP processor module, which generates the web page document. As a general-purpose programming language, PHP code is processed by an interpreter application in command line mode performing desired operating system operations and producing program output on its standard output channel. It may also function as a graphical application. PHP is available as a processor for most modern web servers and as a standalone interpreter on most operating systems and computing platforms. http://en.wikipedia.org/wiki/PHP
    13. 13. Diana’s Rule #1 Programming is asking the right questions and coming up with possible answers.
    14. 14. Diana’s Rule #1 Self educate constantly. Your sixth-grade English teacher was right - if you don’t know, look it up. Find places to get help. Ignore whatever (or whomever) isn’t helpful. Programming is asking the right questions and coming up with possible answers.
    15. 15. I’m a Mac.
    16. 16. Are you a Mac || a PC? I’m a Mac.
    17. 17. Good Places to Start
    18. 18. Good Places to Start • W3Schools Tutorial: http://www.w3schools.com/PHP/
    19. 19. Good Places to Start • W3Schools Tutorial: http://www.w3schools.com/PHP/ • HeadFirst Labs Book: http://headfirstlabs.com/books/ hfphp/
    20. 20. Good Places to Start • W3Schools Tutorial: http://www.w3schools.com/PHP/ • HeadFirst Labs Book: http://headfirstlabs.com/books/ hfphp/ • PHP.net (and tutorial): http://php.net/manual/en/ tutorial.php
    21. 21. Good Places to Start • W3Schools Tutorial: http://www.w3schools.com/PHP/ • HeadFirst Labs Book: http://headfirstlabs.com/books/ hfphp/ • PHP.net (and tutorial): http://php.net/manual/en/ tutorial.php • Pro Drupal Development Book: http:// www.drupalbook.com/
    22. 22. Diana’s Rule #2 How do I get to Carnegie Hall?
    23. 23. Diana’s Rule #2 Write code. How do I get to Carnegie Hall?
    24. 24. Diana’s Rule #2 Write code. Writing code is the only thing that teaches you how to write code. How do I get to Carnegie Hall?
    25. 25. Write Code! I recommend TextMate and Coda for Mac, EditPlus and Aptana for PCs
    26. 26. Write Code! 1. Use Codepad or open Notepad (or any other simple text program). I recommend TextMate and Coda for Mac, EditPlus and Aptana for PCs
    27. 27. Write Code! 1. Use Codepad or open Notepad (or any other simple text program). 2. Not Microsoft Word, never ever ever use Word. A piece of paper and a pen is fine too. I recommend TextMate and Coda for Mac, EditPlus and Aptana for PCs
    28. 28. Write Code! 1. Use Codepad or open Notepad (or any other simple text program). 2. Not Microsoft Word, never ever ever use Word. A piece of paper and a pen is fine too. 2. Write this in the top left-hand corner: I recommend TextMate and Coda for Mac, EditPlus and Aptana for PCs
    29. 29. Write Code! I recommend TextMate and Coda for Mac, EditPlus and Aptana for PCs
    30. 30. <?php /** * About Me */ $name = “Your Name Here”; echo $name; ?>
    31. 31. Bonus point if you’ve seen the movie more than 5 times.
    32. 32. $name = “Trinity”; Bonus point if you’ve seen the movie more than 5 times.
    33. 33. Diana’s Rule #3 “Stupid” mistakes are 85% of mistakes you’ll make.
    34. 34. Diana’s Rule #3 You will suffer daily from the Missing Semicolon Syndrome (MSS). Forgetting the semicolon is the most common syntax error. PHP errors will break your site. Don’t panic! Just fix them. “Stupid” mistakes are 85% of mistakes you’ll make.
    35. 35. Four PHP Concepts
    36. 36. Four PHP Concepts • Variables: containers for your stuff
    37. 37. Four PHP Concepts • Variables: containers for your stuff • Arrays: a variable that is a list, or a list of lists
    38. 38. Four PHP Concepts • Variables: containers for your stuff • Arrays: a variable that is a list, or a list of lists • Conditionals: the brains of your operation
    39. 39. Four PHP Concepts • Variables: containers for your stuff • Arrays: a variable that is a list, or a list of lists • Conditionals: the brains of your operation • Functions: little blocks of “do stuff”
    40. 40. Variables
    41. 41. Variables • begin with the ‘$’ symbol
    42. 42. Variables • begin with the ‘$’ symbol • should be aptly named
    43. 43. Variables • begin with the ‘$’ symbol • should be aptly named • come in 8 different flavors
    44. 44. Variables • begin with the ‘$’ symbol • should be aptly named • come in 8 different flavors • are assigned a value using =
    45. 45. 8 Types of Variables
    46. 46. 8 Types of Variables • Boolean: $fourKitchensWebChef = TRUE;
    47. 47. 8 Types of Variables • Boolean: $fourKitchensWebChef = TRUE; • Integer: $numPets = 3;
    48. 48. 8 Types of Variables • Boolean: $fourKitchensWebChef = TRUE; • Integer: $numPets = 3; • Float: $pie = 3.1416;
    49. 49. 8 Types of Variables • Boolean: $fourKitchensWebChef = TRUE; • Integer: $numPets = 3; • Float: $pie = 3.1416; • String: $hobbies = “I enjoy hiking.”;
    50. 50. 8 Types of Variables • Boolean: $fourKitchensWebChef = TRUE; • Integer: $numPets = 3; • Float: $pie = 3.1416; • String: $hobbies = “I enjoy hiking.”; • Array: $currBooks = array(“The World Without Us”, “Outliers”, “Brain Rules”, “Wolf Hall”);
    51. 51. And The Rest
    52. 52. And The Rest Variables we won’t discuss (much) today:
    53. 53. And The Rest Variables we won’t discuss (much) today: • Object: $node->id
    54. 54. And The Rest Variables we won’t discuss (much) today: • Object: $node->id • Resource: the database connection, for example
    55. 55. And The Rest Variables we won’t discuss (much) today: • Object: $node->id • Resource: the database connection, for example • NULL
    56. 56. 1 point for the group name, 1 point each for individual names
    57. 57. $montyPython = array(“TerryG”, “John”, “Michael”, “TerryJ”, “Eric”, “Graham”); 1 point for the group name, 1 point each for individual names
    58. 58. Arrays array keys start with 0
    59. 59. Arrays $myPets = array(“Maya”, “Cloe”, “August”); echo $myPets[0]; array keys start with 0
    60. 60. Maya Arrays $myPets = array(“Maya”, “Cloe”, “August”); echo $myPets[0]; array keys start with 0
    61. 61. Foreach is your friend $myPets = array(“Maya”, “Cloe”, “August”); foreach ($myPets as $pet) { echo $pet; echo “<br />”; } // $pet is a variable you create
    62. 62. Maya Cloe August Foreach is your friend $myPets = array(“Maya”, “Cloe”, “August”); foreach ($myPets as $pet) { echo $pet; echo “<br />”; } // $pet is a variable you create
    63. 63. Keys and Values $actor = array( ‘name’ => “John Cleese”, ‘nickname’ => “Bruce”, ‘job’ => “Minister of Silly Walks”, ); echo $actor[‘nickname’]; this is very common in Drupal coding
    64. 64. Bruce Keys and Values $actor = array( ‘name’ => “John Cleese”, ‘nickname’ => “Bruce”, ‘job’ => “Minister of Silly Walks”, ); echo $actor[‘nickname’]; this is very common in Drupal coding
    65. 65. <?php /** * About Me */ $name = “Your Name Here”; $yourList = array(“Thing1”, “Thing2”, “Thing3”); ?> Write your own version!
    66. 66. <?php /** * About Me add some */ variables $name = “Your Name Here”; $yourList = array(“Thing1”, “Thing2”, “Thing3”); ?> Write your own version!
    67. 67. <?php /** * About Me add some */ variables $name = “Your Name Here”; $yourList = array(“Thing1”, “Thing2”, “Thing3”); add an array ?> Write your own version!
    68. 68. TRUE || FALSE Bonus point if you shout “Janeway!”
    69. 69. TRUE || FALSE Bonus point if you shout “Janeway!”
    70. 70. TRUE || FALSE Bonus point if you shout “Janeway!”
    71. 71. TRUE || FALSE TRUE Bonus point if you shout “Janeway!”
    72. 72. Symbols to Know More: http://www.w3schools.com/PHP/php_operators.asp
    73. 73. Symbols to Know • Assignment: = More: http://www.w3schools.com/PHP/php_operators.asp
    74. 74. Symbols to Know • Assignment: = • AND: && More: http://www.w3schools.com/PHP/php_operators.asp
    75. 75. Symbols to Know • Assignment: = • AND: && • OR: || More: http://www.w3schools.com/PHP/php_operators.asp
    76. 76. Symbols to Know • Assignment: = • AND: && • OR: || • Greater and Less Than: > < More: http://www.w3schools.com/PHP/php_operators.asp
    77. 77. Symbols to Know • Assignment: = • AND: && • OR: || • Greater and Less Than: > < • Is Equal To: == More: http://www.w3schools.com/PHP/php_operators.asp
    78. 78. Let’s Talk Boolean
    79. 79. Let’s Talk Boolean • My name is Diana
    80. 80. Let’s Talk Boolean • My name is Diana • My name is Diana && I own a motorcycle
    81. 81. Let’s Talk Boolean • My name is Diana • My name is Diana && I own a motorcycle • My name Diana || I am younger than 29
    82. 82. Let’s Talk Boolean • My name is Diana • My name is Diana && I own a motorcycle • My name Diana || I am younger than 29 • (My name is Diana || I am younger than 29) && I own a motorcycle
    83. 83. $myMotorcycle = “BMW F650GS”;
    84. 84. Conditionals are Control if ($captain == “Picard”) { echo “Make it so, Number One.”; } elseif ($captain == “Kirk”) { echo “Two to beam up.”; } else { echo “Please state the nature of your medical emergency.”; }
    85. 85. <?php /** * Boolean Example */ $captain = “Janeway”; if ($captain == “Picard”) { echo “Make it so, Number One.”; } elseif ($captain == “Kirk”) { echo “Two to beam up.”; } else { echo “Please state the nature of your medical emergency.”; ?> There is an MSS issue in this code, can you find it? Hint: }
    86. 86. <?php Please state the nature of your medical emergency. /** * Boolean Example */ $captain = “Janeway”; if ($captain == “Picard”) { echo “Make it so, Number One.”; } elseif ($captain == “Kirk”) { echo “Two to beam up.”; } else { echo “Please state the nature of your medical emergency.”; ?> There is an MSS issue in this code, can you find it? Hint: }
    87. 87. <?php Please state the nature of your medical emergency. /** * Boolean Example */ $captain = “Janeway”; MSS ALERT! if ($captain == “Picard”) { echo “Make it so, Number One.”; } elseif ($captain == “Kirk”) { echo “Two to beam up.”; } else { echo “Please state the nature of your medical emergency.”; ?> There is an MSS issue in this code, can you find it? Hint: }
    88. 88. <?php /** * About Me */ $name = “Your Name Here”; $yourList = array(“Thing1”, “Thing2”, “Thing3”); if (variable == something && variable == something) { print “A clever sentence”; } ?> Write your own version!
    89. 89. <?php /** * About Me */ variables $name = “Your Name Here”; $yourList = array(“Thing1”, “Thing2”, “Thing3”); if (variable == something && variable == something) { print “A clever sentence”; } ?> Write your own version!
    90. 90. <?php /** * About Me */ variables $name = “Your Name Here”; $yourList = array(“Thing1”, “Thing2”, “Thing3”); if (variable == something && variable == something) { print “A clever sentence”; } add a conditional ?> Write your own version!
    91. 91. A bonus point if you’ve read the book && seen the movie
    92. 92. Hello. My name is Inigo Montoya. You killed my father. Prepare to die. A bonus point if you’ve read the book && seen the movie
    93. 93. Functions Functions encapsulate code that does a specific task. The function can be called whenever the task needs to be performed. Tutorial: http://www.tizag.com/phpT/phpfunctions.php
    94. 94. // call the function to count the fingers $killHim = isSixFingeredMan($numFingers); if ($killHim) { echo “Hello. My name is Inigo Montoya. You killed my father. Prepare to die.”; } /** * Returns true if number of fingers is 6 */ function isSixFingeredMan($numFingers) { if ($numFingers == 6) { return TRUE; } else { return FALSE; } }
    95. 95. // call the function to count the fingers $killHim = isSixFingeredMan($numFingers); call if ($killHim) { echo “Hello. My name is Inigo Montoya. You killed my father. Prepare to die.”; } /** * Returns true if number of fingers is 6 */ function isSixFingeredMan($numFingers) { if ($numFingers == 6) { return TRUE; } else { return FALSE; } }
    96. 96. // call the function to count the fingers $killHim = isSixFingeredMan($numFingers); call if ($killHim) { echo “Hello. My name is Inigo Montoya. You killed my father. Prepare to die.”; } /** * Returns true if number of fingers is 6 */ function isSixFingeredMan($numFingers) { if ($numFingers == 6) { return TRUE; } else { function return FALSE; } }
    97. 97. // call the function to count the fingers $killHim = isSixFingeredMan($numFingers); call if ($killHim) { echo “Hello. My name is Inigo Montoya. You killed my father. Prepare to die.”; } continue on /** * Returns true if number of fingers is 6 */ function isSixFingeredMan($numFingers) { if ($numFingers == 6) { return TRUE; } else { function return FALSE; } }
    98. 98. Function Facts
    99. 99. Function Facts • include the word “function”
    100. 100. Function Facts • include the word “function” • should be aptly named
    101. 101. Function Facts • include the word “function” • should be aptly named • can accept one or more parameters
    102. 102. Function Facts • include the word “function” • should be aptly named • can accept one or more parameters • can return something
    103. 103. Function Facts • include the word “function” • should be aptly named • can accept one or more parameters • can return something • should be used for repeated tasks and to organize logic
    104. 104. Function Facts • include the word “function” • should be aptly named • can accept one or more parameters • can return something • should be used for repeated tasks and to organize logic • can be public or private
    105. 105. <?php /** * Function Fun */ $yourVariable = “Something”; $returnedVariable = NULL; $returnedVariable = yourFunction($yourVariable); echo $returnedVariable; public function yourFunction($yourVariable) { $greeting = “Here’s a sentence containing “. $yourVariable; return $greeting; } ?> Write your own version!
    106. 106. <?php /** * Function Fun */ $yourVariable = “Something”; define a variable $returnedVariable = NULL; $returnedVariable = yourFunction($yourVariable); echo $returnedVariable; public function yourFunction($yourVariable) { $greeting = “Here’s a sentence containing “. $yourVariable; return $greeting; } ?> Write your own version!
    107. 107. <?php /** * Function Fun */ $yourVariable = “Something”; define a variable $returnedVariable = NULL; $returnedVariable = yourFunction($yourVariable); echo $returnedVariable; call your function public function yourFunction($yourVariable) { $greeting = “Here’s a sentence containing “. $yourVariable; return $greeting; } ?> Write your own version!
    108. 108. <?php /** * Function Fun */ $yourVariable = “Something”; define a variable $returnedVariable = NULL; $returnedVariable = yourFunction($yourVariable); echo $returnedVariable; call your function public function yourFunction($yourVariable) { $greeting = “Here’s a sentence containing “. $yourVariable; return $greeting; } add a function ?> Write your own version!
    109. 109. <?php /** * Function Fun */ $yourVariable = “Something”; define a variable $returnedVariable = NULL; $returnedVariable = yourFunction($yourVariable); echo $returnedVariable; public function yourFunction($yourVariable) { $greeting = “Here’s a sentence containing “. $yourVariable; return $greeting; } add a function ?> Write your own version!
    110. 110. <?php /** * Function Fun */ $yourVariable = “Something”; define a variable $returnedVariable = NULL; $returnedVariable = yourFunction($yourVariable); echo $returnedVariable; continue on public function yourFunction($yourVariable) { $greeting = “Here’s a sentence containing “. $yourVariable; return $greeting; } add a function ?> Write your own version!
    111. 111. <?php Here’s a sentence containing Something. /** * Function Fun */ $yourVariable = “Something”; define a variable $returnedVariable = NULL; $returnedVariable = yourFunction($yourVariable); echo $returnedVariable; continue on public function yourFunction($yourVariable) { $greeting = “Here’s a sentence containing “. $yourVariable; return $greeting; } add a function ?> Write your own version!
    112. 112. What does he do for a living? Score one point for each title you can name
    113. 113. Diana’s Rule #4 Practice makes a programmer.
    114. 114. Diana’s Rule #4 Read and dissect (good) code. Practice makes a programmer.
    115. 115. Diana’s Rule #5 http://drupal.org/node/287350
    116. 116. Diana’s Rule #5 Follow best practices. Like the rules of the road, best practices prevent (coding) accidents. They also make you a likable person. http://drupal.org/node/287350
    117. 117. Browser Points Modern Browsers Only!
    118. 118. Browser Points • +10: Firefox && (Chrome || Opera) Modern Browsers Only!
    119. 119. Browser Points • +10: Firefox && (Chrome || Opera) • +8: Firefox && (IE 7 || IE 8 || Safari) Modern Browsers Only!
    120. 120. Browser Points • +10: Firefox && (Chrome || Opera) • +8: Firefox && (IE 7 || IE 8 || Safari) • +5: Firefox Modern Browsers Only!
    121. 121. Browser Points • +10: Firefox && (Chrome || Opera) • +8: Firefox && (IE 7 || IE 8 || Safari) • +5: Firefox • +3: IE 7 || IE 8 Modern Browsers Only!
    122. 122. Browser Points • +10: Firefox && (Chrome || Opera) • +8: Firefox && (IE 7 || IE 8 || Safari) • +5: Firefox • +3: IE 7 || IE 8 • +1: Safari Modern Browsers Only!
    123. 123. Browser Points • +10: Firefox && (Chrome || Opera) • +8: Firefox && (IE 7 || IE 8 || Safari) • +5: Firefox • +3: IE 7 || IE 8 • +1: Safari • -3,890,745: IE 6 Modern Browsers Only!
    124. 124. Diana’s Five Rules 1. Constantly self educate. 2. Write code. 3. Accept Missing Semicolon Syndrome gracefully. 4. Read and dissect (good) code. 5. Follow best practices. diana@fourkitchens.com
    125. 125. Take the Survey http://sf2010.drupal.org/conference/schedule

    ×