Introduction to PHP
         Bradley Holt (http://bradley-holt.com/) &
Matthew Weier O’Phinney (http:/ /weierophinney.net/...
What is PHP?


Loosely typed scripting language

Interpreted at runtime (use an opcode cache)

Commonly used to build web ...
Who uses PHP?


Yahoo!

Facebook

20+ million other domain names
Brief History
Personal Home Page /
  Forms Interpreter

Created by Rasmus Lerdorf

PHP/FI 1.0 released in 1995

PHP/FI 2.0 released in 1...
PHP: Hypertext
     Preprocessor

Created by Andi Gutmans and Zeev Suraski

PHP 3.0 released in 1998

PHP 4.4 released in ...
PHP 5


New object model

PHP 5.0 released in 2004

PHP 5.3 released in 2009
Syntax
Hello World

<?php
// hello.php
echo 'Hello, VT Code Camp.';
?>
Variable Assignment


<?php
$hello = 'Hello, VT Code Camp.';
echo $hello;
Comments
One Line Comments


<?php
// A one line comment
# Another one line comment
Multi-Line Comments

<?php
/*
A multi-line comment
*/
DocBlock Comments
<?php
/**
 * This function does nothing
 *
 * @param string $bar
 * @return void
 */
function foo($bar) ...
Primitive Data Types

<?php
$isPhpProgrammer = true; // boolean
$howOldIsPhp = 15; // integer
$pi = 3.14; // float
$event ...
Conditionals
If

<?php
if (true) {
    echo 'Yes';
}
If-Then-Else
<?php
if (false) {
    echo 'No';
} else {
    echo 'Yes';
}
If-Then-Else-If
<?php
if (false) {
    echo 'No';
} elseif (false) {
    echo 'No';
} else {
    echo 'Yes';
}
Switch
<?php
switch ('PHP') {
    case 'Ruby':
        echo 'No';
        break;
    case 'PHP':
        echo 'Yes';
     ...
Operators
Arithmetic
<?php
$a = 10;
$b = $a +   1;   //   11
$c = $a -   1;   //   9
$d = $a *   5;   //   50
$e = $a /   2;   //   ...
String Concatenation


<?php
$myString = 'foo' . 'bar'; // foobar
$myString .= 'baz'; // foobarbaz
Comparison
Equivalence

<?php
if (2 == 3) { echo 'No'; }
if (3 == '3') { echo 'Yes'; }
if (2 != 3) { echo 'Yes'; }
Identity

<?php
if (3 === '3') { echo 'No'; }
if (3 === 3) { echo 'Yes'; }
if (3 !== 4) { echo 'Yes'; }
Logical Operators
<?php
// NOT
if (!true) { echo 'No'; }
// AND
if (true && false) { echo 'No'; }
// OR
if (true || false)...
Strings & Interpolation
Literal Single Quotes

<?php
$x = 2;
echo 'I ate $x cookies.';
// I ate $x cookies.
Double Quotes

<?php
$x = 2;
echo "I ate $x cookies.";
// I ate 2 cookies.
Literal Double Quotes

<?php
$x = 2;
echo "I ate $x cookies.";
// I ate $x cookies.
Curly Brace
         Double Quotes

<?php
$x = 2;
echo "I ate {$x} cookies.";
// I ate 2 cookies.
Constants
Defining


<?php
define('HELLO', 'Hello, Code Camp');
echo HELLO; // Hello, Code Camp
As of PHP 5.3


<?php
const HELLO = 'Hello, Code Camp';
echo HELLO; // Hello, Code Camp
Arrays
Enumerative
Automatic Indexing


<?php
$foo[] = 'bar'; // [0] => bar
$foo[] = 'baz'; // [1] => baz
Explicit Indexing


<?php
$foo[0] = 'bar'; // [0] => bar
$foo[1] = 'baz'; // [1] => baz
Array Construct with
     Automatic Indexing
<?php
$foo = array(
    'bar', // [0] => bar
    'baz', // [1] => baz
);
Array Construct with
      Explicit Indexing
<?php
$foo = array(
    0 => 'bar', // [0] => bar
    1 => 'baz', // [1] => b...
Array Construct with
     Arbitrary Indexing
<?php
$foo = array(
    1 => 'bar', // [1] => bar
    2 => 'baz', // [2] => b...
Associative
Explicit Indexing


<?php
$foo['a'] = 'bar'; // [a] => bar
$foo['b'] = 'baz'; // [b] => baz
Array Construct

<?php
$foo = array(
    'a' => 'bar', // [a] => bar
    'b' => 'baz', // [b] => baz
);
Iterators
While
<?php
$x = 0;
while ($x < 5) {
    echo '.';
    $x++;
}
For

<?php
for ($x = 0; $x < 5; $x++) {
    echo '.';
}
Foreach

<?php
$x = array(0, 1, 2, 3, 4);
foreach ($x as $y) {
    echo $y;
}
Foreach Key/Value Pairs
<?php
$talks = array(
    'php' => 'Intro to PHP',
    'ruby' => 'Intro to Ruby',
);
foreach ($tal...
Functions
Built-in
<?php
echo strlen('Hello'); // 5
echo trim(' Hello '); // Hello
echo count(array(0, 1, 2, 3)); // 4
echo uniqid()...
User-Defined
<?php
function add($x, $y)
{
    return $x + $y;
}

echo add(2, 4); // 6
Anonymous Functions /
Closures (since PHP 5.3)
Variable Assignment
<?php
$sayHi = function ()
{
    return 'Hi';
};

echo $sayHi(); // Hi
Callbacks
<?php
$values = array(3, 7, 2);
usort($values, function ($a, $b) {
    if ($a == $b) { return 0; }
    return ($...
Classes & Objects
Class Declaration

<?php
class Car
{
}
Property Declaration

<?php
class Car
{
    private $_hasSunroof = true;
}
Method Declaration
<?php
class Car
{
    public function hasSunroof()
    {
        return $this->_hasSunroof;
    }
}
Class Constants
<?php
class Car
{
    const ENGINE_V4 = 'V4';
    const ENGINE_V6 = 'V6';
    const ENGINE_V8 = 'V8';
}

e...
Object Instantiation
      & Member Access
<?php
$myCar = new Car();
if ($myCar->hasSunroof()) {
    echo 'Yay!';
}
Class Inheritance

<?php
class Chevy extends Car
{
}
Interfaces

<?php
interface Vehicle
{
    public function hasSunroof();
}
Implementing Interfaces
<?php
class Car implements Vehicle
{
    public function hasSunroof()
    {
        return $this->...
Member Visibility
Public



Default visibility

Visible everywhere
Protected


Visible to child classes

Visible to the object itself

Visible to other objects of the same type
Private



Visible to the object itself

Visible within the defining class declaration
Tools
IDEs
Eclipse (PDT, Zend Studio, Aptana)

NetBeans

PHPStorm

Emacs

Vim

Many more…
Frameworks
Zend Framework

Symfony

CodeIgniter

Agavi

CakePHP

Many more…
PEAR


PHP Extension and Application Repository

Package manager

PECL (PHP Extension Community Library)
Miscellaneous Tools
PHPUnit

phpDocumentor

Phing

PHP CodeSniffer

PHP Mess Detector

phpUnderControl
Example PHP Scripts
http://github.com/bradley-holt/introduction-to-php
Questions?
Thank You
         Bradley Holt (http://bradley-holt.com/) &
Matthew Weier O’Phinney (http:/ /weierophinney.net/matthew/)
...
Upcoming SlideShare
Loading in...5
×

Introduction to PHP

36,166

Published on

An introduction to the PHP programming language.

Published in: Technology
3 Comments
23 Likes
Statistics
Notes
  • IT Pathshala, Noida-Sec-63, offers Guaranteed JOB with PHP Training Programs.

    PAY US ONLY IF YOU GET A JOB!

    CALL US - +91-9871350999 or +91-8800394970

    Technology Centre Address: H-110, Sector-63, Noida (UP)

    Program Highlights:

    - 100% Guaranteed Job – Don’t pay us if you don’t get a JOB!
    - Industry Ready Courses
    - LIVE International Project Experience
    - Exposure to corporate culture at our IT services facility
    - Networking & Interaction with Industry Experts
    - Training provided by reputed IT Company
    - Live Project Source Code made available in CD
    - Technology certification and experience letter at program completion
    - Online Study Content
    Program Takeaways:

    - GUARANTEED JOB in your technology domain
    - Live Major Project development
    - Experience Letter on Training Completion
    - Certification on Training Completion
    - Project Source Code available in CD

    CALL US - +91-9871350999 or +91-8800394970

    Our website: www.itpathshala.com
       Reply 
    Are you sure you want to  Yes  No
    Your message goes here
  • Hello, PHP stands for ‘Hypertext Preprocessor’, which is widely used for making dynamic and interactive web pages. PHP is server side scripting language intended to help web developers build dynamic web pages. PHP is the widely-used, free, and efficient alternative to other server scripting languages............. for more details please check out this link..... http://mindstick.com/Articles/33bccb35-3934-4f9e-b89f-e94a434653cc/?Introduction%20of%20PHP

    Thanks !!!!!!!!
       Reply 
    Are you sure you want to  Yes  No
    Your message goes here
  • Nicely done.
       Reply 
    Are you sure you want to  Yes  No
    Your message goes here
No Downloads
Views
Total Views
36,166
On Slideshare
0
From Embeds
0
Number of Embeds
15
Actions
Shares
0
Downloads
1,342
Comments
3
Likes
23
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
  1. A particular slide catching your eye?

    Clipping is a handy way to collect important slides you want to go back to later.

×