Introduction to PHP

44,208 views
43,631 views

Published on

An introduction to the PHP programming language.

Published in: Technology
8 Comments
42 Likes
Statistics
Notes
  • More than 5000 IT Certified ( SAP,Oracle,Mainframe,Microsoft and IBM Technologies etc...)Consultants registered. Register for IT courses at http://www.todaycourses.com Most of our companies will help you in processing H1B Visa, Work Permit and Job Placements
       Reply 
    Are you sure you want to  Yes  No
    Your message goes here
  • Modern PHP: New Features and Good Practices --- http://amzn.to/1SaRPLI
       Reply 
    Are you sure you want to  Yes  No
    Your message goes here
  • Learning PHP, MySQL & JavaScript: With jQuery, CSS & HTML5 (Learning Php, Mysql, Javascript, Css & Html5) --- http://amzn.to/1SaRSag
       Reply 
    Are you sure you want to  Yes  No
    Your message goes here
  • PhP: Learn PHP Programming Quick & Easy --- http://amzn.to/1PkaFeE
       Reply 
    Are you sure you want to  Yes  No
    Your message goes here
  • PHP’s popularity is diminishing slowly. In this article, we will discuss whether or not PHP language is dying with the help of statistics because stats don’t tell lie!!! check out this slide :http://www.slideshare.net/PixelCrayons/is-php-dying
       Reply 
    Are you sure you want to  Yes  No
    Your message goes here
No Downloads
Views
Total views
44,208
On SlideShare
0
From Embeds
0
Number of Embeds
7,481
Actions
Shares
0
Downloads
2,120
Comments
8
Likes
42
Embeds 0
No embeds

No notes for slide

Introduction to PHP

  1. 1. Introduction to PHP Bradley Holt (http://bradley-holt.com/) & Matthew Weier O’Phinney (http:/ /weierophinney.net/matthew/) Feedback: http://joind.in/1976
  2. 2. What is PHP? Loosely typed scripting language Interpreted at runtime (use an opcode cache) Commonly used to build web applications
  3. 3. Who uses PHP? Yahoo! Facebook 20+ million other domain names
  4. 4. Brief History
  5. 5. Personal Home Page / Forms Interpreter Created by Rasmus Lerdorf PHP/FI 1.0 released in 1995 PHP/FI 2.0 released in 1997
  6. 6. PHP: Hypertext Preprocessor Created by Andi Gutmans and Zeev Suraski PHP 3.0 released in 1998 PHP 4.4 released in 2005
  7. 7. PHP 5 New object model PHP 5.0 released in 2004 PHP 5.3 released in 2009
  8. 8. Syntax
  9. 9. Hello World <?php // hello.php echo 'Hello, VT Code Camp.'; ?>
  10. 10. Variable Assignment <?php $hello = 'Hello, VT Code Camp.'; echo $hello;
  11. 11. Comments
  12. 12. One Line Comments <?php // A one line comment # Another one line comment
  13. 13. Multi-Line Comments <?php /* A multi-line comment */
  14. 14. DocBlock Comments <?php /** * This function does nothing * * @param string $bar * @return void */ function foo($bar) {}
  15. 15. Primitive Data Types <?php $isPhpProgrammer = true; // boolean $howOldIsPhp = 15; // integer $pi = 3.14; // float $event = 'VT Code Camp'; // string
  16. 16. Conditionals
  17. 17. If <?php if (true) { echo 'Yes'; }
  18. 18. If-Then-Else <?php if (false) { echo 'No'; } else { echo 'Yes'; }
  19. 19. If-Then-Else-If <?php if (false) { echo 'No'; } elseif (false) { echo 'No'; } else { echo 'Yes'; }
  20. 20. Switch <?php switch ('PHP') { case 'Ruby': echo 'No'; break; case 'PHP': echo 'Yes'; break; }
  21. 21. Operators
  22. 22. Arithmetic <?php $a = 10; $b = $a + 1; // 11 $c = $a - 1; // 9 $d = $a * 5; // 50 $e = $a / 2; // 5 $f = $a % 3; // 1
  23. 23. String Concatenation <?php $myString = 'foo' . 'bar'; // foobar $myString .= 'baz'; // foobarbaz
  24. 24. Comparison
  25. 25. Equivalence <?php if (2 == 3) { echo 'No'; } if (3 == '3') { echo 'Yes'; } if (2 != 3) { echo 'Yes'; }
  26. 26. Identity <?php if (3 === '3') { echo 'No'; } if (3 === 3) { echo 'Yes'; } if (3 !== 4) { echo 'Yes'; }
  27. 27. Logical Operators <?php // NOT if (!true) { echo 'No'; } // AND if (true && false) { echo 'No'; } // OR if (true || false) { echo 'No'; }
  28. 28. Strings & Interpolation
  29. 29. Literal Single Quotes <?php $x = 2; echo 'I ate $x cookies.'; // I ate $x cookies.
  30. 30. Double Quotes <?php $x = 2; echo "I ate $x cookies."; // I ate 2 cookies.
  31. 31. Literal Double Quotes <?php $x = 2; echo "I ate $x cookies."; // I ate $x cookies.
  32. 32. Curly Brace Double Quotes <?php $x = 2; echo "I ate {$x} cookies."; // I ate 2 cookies.
  33. 33. Constants
  34. 34. Defining <?php define('HELLO', 'Hello, Code Camp'); echo HELLO; // Hello, Code Camp
  35. 35. As of PHP 5.3 <?php const HELLO = 'Hello, Code Camp'; echo HELLO; // Hello, Code Camp
  36. 36. Arrays
  37. 37. Enumerative
  38. 38. Automatic Indexing <?php $foo[] = 'bar'; // [0] => bar $foo[] = 'baz'; // [1] => baz
  39. 39. Explicit Indexing <?php $foo[0] = 'bar'; // [0] => bar $foo[1] = 'baz'; // [1] => baz
  40. 40. Array Construct with Automatic Indexing <?php $foo = array( 'bar', // [0] => bar 'baz', // [1] => baz );
  41. 41. Array Construct with Explicit Indexing <?php $foo = array( 0 => 'bar', // [0] => bar 1 => 'baz', // [1] => baz );
  42. 42. Array Construct with Arbitrary Indexing <?php $foo = array( 1 => 'bar', // [1] => bar 2 => 'baz', // [2] => baz );
  43. 43. Associative
  44. 44. Explicit Indexing <?php $foo['a'] = 'bar'; // [a] => bar $foo['b'] = 'baz'; // [b] => baz
  45. 45. Array Construct <?php $foo = array( 'a' => 'bar', // [a] => bar 'b' => 'baz', // [b] => baz );
  46. 46. Iterators
  47. 47. While <?php $x = 0; while ($x < 5) { echo '.'; $x++; }
  48. 48. For <?php for ($x = 0; $x < 5; $x++) { echo '.'; }
  49. 49. Foreach <?php $x = array(0, 1, 2, 3, 4); foreach ($x as $y) { echo $y; }
  50. 50. Foreach Key/Value Pairs <?php $talks = array( 'php' => 'Intro to PHP', 'ruby' => 'Intro to Ruby', ); foreach ($talks as $id => $name) { echo "$name is talk ID $id."; echo PHP_EOL; }
  51. 51. Functions
  52. 52. Built-in <?php echo strlen('Hello'); // 5 echo trim(' Hello '); // Hello echo count(array(0, 1, 2, 3)); // 4 echo uniqid(); // 4c8a6660519d5 echo mt_rand(0, 9); // 3 echo serialize(42); // i:42; echo json_encode(array('a' => 'b')); // {"a":"b"}
  53. 53. User-Defined <?php function add($x, $y) { return $x + $y; } echo add(2, 4); // 6
  54. 54. Anonymous Functions / Closures (since PHP 5.3)
  55. 55. Variable Assignment <?php $sayHi = function () { return 'Hi'; }; echo $sayHi(); // Hi
  56. 56. Callbacks <?php $values = array(3, 7, 2); usort($values, function ($a, $b) { if ($a == $b) { return 0; } return ($a < $b) ? -1 : 1; }); /* [0] => 2 [1] => 3 [2] => 7 */
  57. 57. Classes & Objects
  58. 58. Class Declaration <?php class Car { }
  59. 59. Property Declaration <?php class Car { private $_hasSunroof = true; }
  60. 60. Method Declaration <?php class Car { public function hasSunroof() { return $this->_hasSunroof; } }
  61. 61. Class Constants <?php class Car { const ENGINE_V4 = 'V4'; const ENGINE_V6 = 'V6'; const ENGINE_V8 = 'V8'; } echo Car::ENGINE_V6; // V6
  62. 62. Object Instantiation & Member Access <?php $myCar = new Car(); if ($myCar->hasSunroof()) { echo 'Yay!'; }
  63. 63. Class Inheritance <?php class Chevy extends Car { }
  64. 64. Interfaces <?php interface Vehicle { public function hasSunroof(); }
  65. 65. Implementing Interfaces <?php class Car implements Vehicle { public function hasSunroof() { return $this->_hasSunroof; } }
  66. 66. Member Visibility
  67. 67. Public Default visibility Visible everywhere
  68. 68. Protected Visible to child classes Visible to the object itself Visible to other objects of the same type
  69. 69. Private Visible to the object itself Visible within the defining class declaration
  70. 70. Tools
  71. 71. IDEs Eclipse (PDT, Zend Studio, Aptana) NetBeans PHPStorm Emacs Vim Many more…
  72. 72. Frameworks Zend Framework Symfony CodeIgniter Agavi CakePHP Many more…
  73. 73. PEAR PHP Extension and Application Repository Package manager PECL (PHP Extension Community Library)
  74. 74. Miscellaneous Tools PHPUnit phpDocumentor Phing PHP CodeSniffer PHP Mess Detector phpUnderControl
  75. 75. Example PHP Scripts http://github.com/bradley-holt/introduction-to-php
  76. 76. Questions?
  77. 77. Thank You Bradley Holt (http://bradley-holt.com/) & Matthew Weier O’Phinney (http:/ /weierophinney.net/matthew/) Feedback: http://joind.in/1976

×