Successfully reported this slideshow.
We use your LinkedIn profile and activity data to personalize ads and to show you more relevant ads. You can change your ad preferences anytime.

PHP Underground Session 1: The Basics


Published on

Published in: Education, Technology
  • Be the first to comment

PHP Underground Session 1: The Basics

  1. 1. Underground PHP  by Rob Hawkes
  2. 2. The ground rules
  3. 3. What the sessions are ✽ Somewhere to admit lack of understanding ✽ Covering areas missed in workshops ✽ Helping you understand programming theory – critical! ✽ Focussing on the “why” over “how” ✽ A launch pad for further learning
  4. 4. And what they aren’t ✽ Quick fix for The Station • Will provide examples of when to use techniques in context • Will not provide code to copy & paste ✽ Easy • PHP takes time & effort to learn
  5. 5. These go without saying ✽ Provide your own equipment • We might not always be in the labs • I can’t provide individual web space ✽ Don’t piss about • Please only come if you genuinely want to learn • I won’t waste my time or yours
  7. 7. Session 1: The Basics
  8. 8. What we’ll cover ✽ An introduction to PHP ✽ Where to get help when you’re stuck ✽ Best practice when coding ✽ Fundamental knowledge
  9. 9. PHP: An introduction
  10. 10. What is PHP? ✽ Stands for PHP: Hypertext Preprocessor • Yes, a recursive acronym. Funky! ✽ Run directly on the web server • Windows, Linux, and Mac ✽ Makes web pages dynamic • Calculations, database interaction, etc. ✽ Latest version is PHP 5 • Introduces features not supported in PHP 4
  11. 11. Basic PHP process User's Browser Web Server (Apache) PHP Engine MySQL Database
  12. 12. Where to get help
  13. 13. Online help ✽ Official PHP documentation • ✽ Stack Overflow • ✽ SitePoint forums • ✽ Google
  14. 14. Offline help ✽ Don’t look at me • Would love to but I don’t have the time ✽ Books • PHP and MySQL for Dynamic Web Sites • PHP and MySQL Bible
  15. 15. Best practice
  16. 16. Indentation & spacing ✽ Neat & tidy ✽ Makes code readable ✽ Less prone to error ✽ Easier to debug in future
  17. 17. Indentation & spacing Unindented Indented <?php <?php $array=array(‘1’,‘2’,‘3’,‘4’,‘5’); $array = array(‘1’, ‘2’, ‘3’, ‘4’, ‘5’); $count=count($array); $count = count($array); for($i=0;$i<$count;$i++){ if($i==2){ for ($i = 0; $i < $count; $i++) { echo $array[$i]; if ($i == 2) { } echo $array[$i]; } } ?> } ?>
  18. 18. Unindented Indented
  19. 19. Comment everything ✽ // comments a single line ✽ /* text */ comments multiple lines ✽ Helps with learning ✽ Make comments meaningful • Explain in detail exactly what the code does ✽ Saves time when debugging ✽ Will save your arse many times
  20. 20. Naming conventions ✽ camelCase • Always start with a lowercase letter • Capitalise the first letter of any further words ✽ Meaningful names • ShowProfile() rather than function1() • Makes code readable at a glance
  21. 21. Further reading ✽ Zend Framework coding standards • standard.html
  22. 22. Fundamental knowledge
  23. 23. General syntax ✽ Wrap all PHP within <?php and ?> tags • Tells server that the code within is to be run through the PHP system ✽ End every line with a semicolon; • Ok, not every line but enough to use that rule • Lets the PHP system know where a line of code ends • Most rookie errors are caused by forgetting semicolons
  24. 24. General syntax ✽ Use $variables to store data for later • Variables are always preceded with a $ symbol • They can’t begin with a number • Any type of data can be stored inside a variable ✽ Echo and print • Used to output data to the browser • Negligible difference in performance between the two • Most programmers use echo
  25. 25. Data types Name Short name Notes Boolean bool Truth value, either true or false Integer int Whole number (eg. 5) Floating point float / double Decimal fraction (eg. 1.7) String str Series of characters, usually text, enclosed in quotation marks Array arr Used to store multiple pieces of data in one place
  26. 26. Assignment operators Operator Example Notes = $a = 1+4; Assigns the left operand (variable) to the value on the right += $a += 5 Adds the value on the right the existing value of $a -= $a -= 5 Subtracts the value on the right from the existing value of $a .= $a .= ‘Hello’ Appends the value on the right to the existing value of $a, mainly used with strings
  27. 27. Arithmetic operators Operator Name Notes -$a Negation Opposite / reverse of $a $a + $b Addition Sum of $a and $b $a - $b Subtraction Difference of $a and $b $a * $b Multiplication Product of $a and $b $a / $b Division Quotient of $a and $b $a % $b Modulus Remainder of $a divided by $b
  28. 28. Comparison operators Operator Name Notes $a == $b Equal True if $a is equal to $b $a === $b Identical True if $a is equal to $b, and are of the same data type $a != $b Not equal True if $a is not equal to $b $a !== $b Not identical True if $a is not equal to $b, or they are not of the same data type $a < $b Less than True if $a is less than $b $a > $b Greater than True if $a is greater than $b $a <= $b Less than or True if $a is less than or equal to $b equal to $a >= $b Greater than or True if $a is greater than or equal to equal to $b
  29. 29. Increment & decrement Operator Name Notes ++$a Pre-increment Increase $a by one then return $a $a++ Post-increment Return $a then increase $a by one --$a Pre-decrement Decrease $a by one then return $a $a-- Post-decrement Return $a then decrease $a by one
  30. 30. Logic operators Operator Name Notes $a and $b And True if both $a and $b are true $a && $b And True if both $a and $b are true $a or $b Or True if $a or $b is true $a || $b Or True if $a or $b is true $a xor $b Xor True if $a or $b is true, but not both !$a Not True if $a is not true
  31. 31. Operator roundup ✽ PHP uses the same operators as most major programming languages ✽ = does not mean ‘equal to’ • = is used to assign a value • == means ‘equal to’
  32. 32. Further reading ✽ data types documentation • language.types.intro.php ✽ operator documentation • language.operators.php ✽ type comparison tables • types.comparisons.php
  33. 33. Any questions?