SlideShare a Scribd company logo
1 of 77
Download to read offline
Introduction to PHP
         Bradley Holt (http://bradley-holt.com/) &
Matthew Weier O’Phinney (http:/ /weierophinney.net/matthew/)

        Feedback: http://joind.in/1976
What is PHP?


Loosely typed scripting language

Interpreted at runtime (use an opcode cache)

Commonly used to build web applications
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 1997
PHP: Hypertext
     Preprocessor

Created by Andi Gutmans and Zeev Suraski

PHP 3.0 released in 1998

PHP 4.4 released in 2005
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 = 'VT Code Camp'; // string
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';
        break;
}
Operators
Arithmetic
<?php
$a = 10;
$b = $a +   1;   //   11
$c = $a -   1;   //   9
$d = $a *   5;   //   50
$e = $a /   2;   //   5
$f = $a %   3;   //   1
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) { echo 'No'; }
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] => baz
);
Array Construct with
     Arbitrary Indexing
<?php
$foo = array(
    1 => 'bar', // [1] => bar
    2 => 'baz', // [2] => baz
);
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 ($talks as $id => $name) {
    echo "$name is talk ID $id.";
    echo PHP_EOL;
}
Functions
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"}
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 ($a < $b) ? -1 : 1;
});
/* [0] => 2
    [1] => 3
    [2] => 7 */
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';
}

echo Car::ENGINE_V6; // V6
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->_hasSunroof;
    }
}
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/)

        Feedback: http://joind.in/1976

More Related Content

What's hot

Data types in php
Data types in phpData types in php
Data types in php
ilakkiya
 
Php Using Arrays
Php Using ArraysPhp Using Arrays
Php Using Arrays
mussawir20
 

What's hot (20)

Php
PhpPhp
Php
 
PHP - Introduction to PHP Cookies and Sessions
PHP - Introduction to PHP Cookies and SessionsPHP - Introduction to PHP Cookies and Sessions
PHP - Introduction to PHP Cookies and Sessions
 
PHP Cookies and Sessions
PHP Cookies and SessionsPHP Cookies and Sessions
PHP Cookies and Sessions
 
Php mysql
Php mysqlPhp mysql
Php mysql
 
JavaScript - Chapter 5 - Operators
 JavaScript - Chapter 5 - Operators JavaScript - Chapter 5 - Operators
JavaScript - Chapter 5 - Operators
 
Php introduction
Php introductionPhp introduction
Php introduction
 
4.2 PHP Function
4.2 PHP Function4.2 PHP Function
4.2 PHP Function
 
Php and MySQL
Php and MySQLPhp and MySQL
Php and MySQL
 
Javascript
JavascriptJavascript
Javascript
 
Data types in php
Data types in phpData types in php
Data types in php
 
Php Using Arrays
Php Using ArraysPhp Using Arrays
Php Using Arrays
 
Introduction to JavaScript Basics.
Introduction to JavaScript Basics.Introduction to JavaScript Basics.
Introduction to JavaScript Basics.
 
Introduction to JavaScript
Introduction to JavaScriptIntroduction to JavaScript
Introduction to JavaScript
 
PHP - Introduction to File Handling with PHP
PHP -  Introduction to  File Handling with PHPPHP -  Introduction to  File Handling with PHP
PHP - Introduction to File Handling with PHP
 
Javascript arrays
Javascript arraysJavascript arrays
Javascript arrays
 
Control Structures In Php 2
Control Structures In Php 2Control Structures In Php 2
Control Structures In Php 2
 
Arrays in PHP
Arrays in PHPArrays in PHP
Arrays in PHP
 
Php variables
Php variablesPhp variables
Php variables
 
Chapter 02 php basic syntax
Chapter 02   php basic syntaxChapter 02   php basic syntax
Chapter 02 php basic syntax
 
PHP Presentation
PHP PresentationPHP Presentation
PHP Presentation
 

Similar to Introduction to PHP

GettingStartedWithPHP
GettingStartedWithPHPGettingStartedWithPHP
GettingStartedWithPHP
Nat Weerawan
 
2014 database - course 2 - php
2014 database - course 2 - php2014 database - course 2 - php
2014 database - course 2 - php
Hung-yu Lin
 

Similar to Introduction to PHP (20)

Introduction to PHP Lecture 1
Introduction to PHP Lecture 1Introduction to PHP Lecture 1
Introduction to PHP Lecture 1
 
GettingStartedWithPHP
GettingStartedWithPHPGettingStartedWithPHP
GettingStartedWithPHP
 
Php mysql
Php mysqlPhp mysql
Php mysql
 
[PL] Jak nie zostać "programistą" PHP?
[PL] Jak nie zostać "programistą" PHP?[PL] Jak nie zostać "programistą" PHP?
[PL] Jak nie zostać "programistą" PHP?
 
My cool new Slideshow!
My cool new Slideshow!My cool new Slideshow!
My cool new Slideshow!
 
slidesharenew1
slidesharenew1slidesharenew1
slidesharenew1
 
What's new with PHP7
What's new with PHP7What's new with PHP7
What's new with PHP7
 
Web 8 | Introduction to PHP
Web 8 | Introduction to PHPWeb 8 | Introduction to PHP
Web 8 | Introduction to PHP
 
Web Technology_10.ppt
Web Technology_10.pptWeb Technology_10.ppt
Web Technology_10.ppt
 
The new features of PHP 7 - Enrico Zimuel - Codemotion Milan 2016
The new features of PHP 7 - Enrico Zimuel - Codemotion Milan 2016The new features of PHP 7 - Enrico Zimuel - Codemotion Milan 2016
The new features of PHP 7 - Enrico Zimuel - Codemotion Milan 2016
 
The new features of PHP 7
The new features of PHP 7The new features of PHP 7
The new features of PHP 7
 
Php mysql
Php mysqlPhp mysql
Php mysql
 
Php Tutorials for Beginners
Php Tutorials for BeginnersPhp Tutorials for Beginners
Php Tutorials for Beginners
 
php AND MYSQL _ppt.pdf
php AND MYSQL _ppt.pdfphp AND MYSQL _ppt.pdf
php AND MYSQL _ppt.pdf
 
Php hacku
Php hackuPhp hacku
Php hacku
 
Mastering Namespaces in PHP
Mastering Namespaces in PHPMastering Namespaces in PHP
Mastering Namespaces in PHP
 
Php Lecture Notes
Php Lecture NotesPhp Lecture Notes
Php Lecture Notes
 
関西PHP勉強会 php5.4つまみぐい
関西PHP勉強会 php5.4つまみぐい関西PHP勉強会 php5.4つまみぐい
関西PHP勉強会 php5.4つまみぐい
 
2014 database - course 2 - php
2014 database - course 2 - php2014 database - course 2 - php
2014 database - course 2 - php
 
Php with my sql
Php with my sqlPhp with my sql
Php with my sql
 

More from Bradley Holt

Domain-Driven Design at ZendCon 2012
Domain-Driven Design at ZendCon 2012Domain-Driven Design at ZendCon 2012
Domain-Driven Design at ZendCon 2012
Bradley Holt
 

More from Bradley Holt (16)

Domain-Driven Design at ZendCon 2012
Domain-Driven Design at ZendCon 2012Domain-Driven Design at ZendCon 2012
Domain-Driven Design at ZendCon 2012
 
Domain-Driven Design
Domain-Driven DesignDomain-Driven Design
Domain-Driven Design
 
Entity Relationships in a Document Database at CouchConf Boston
Entity Relationships in a Document Database at CouchConf BostonEntity Relationships in a Document Database at CouchConf Boston
Entity Relationships in a Document Database at CouchConf Boston
 
CouchConf NYC CouchApps
CouchConf NYC CouchAppsCouchConf NYC CouchApps
CouchConf NYC CouchApps
 
ZendCon 2011 UnCon Domain-Driven Design
ZendCon 2011 UnCon Domain-Driven DesignZendCon 2011 UnCon Domain-Driven Design
ZendCon 2011 UnCon Domain-Driven Design
 
ZendCon 2011 Learning CouchDB
ZendCon 2011 Learning CouchDBZendCon 2011 Learning CouchDB
ZendCon 2011 Learning CouchDB
 
jQuery Conference Boston 2011 CouchApps
jQuery Conference Boston 2011 CouchAppsjQuery Conference Boston 2011 CouchApps
jQuery Conference Boston 2011 CouchApps
 
OSCON 2011 CouchApps
OSCON 2011 CouchAppsOSCON 2011 CouchApps
OSCON 2011 CouchApps
 
OSCON 2011 Learning CouchDB
OSCON 2011 Learning CouchDBOSCON 2011 Learning CouchDB
OSCON 2011 Learning CouchDB
 
Load Balancing with Apache
Load Balancing with ApacheLoad Balancing with Apache
Load Balancing with Apache
 
CouchDB at New York PHP
CouchDB at New York PHPCouchDB at New York PHP
CouchDB at New York PHP
 
Intermediate PHP
Intermediate PHPIntermediate PHP
Intermediate PHP
 
New Features in PHP 5.3
New Features in PHP 5.3New Features in PHP 5.3
New Features in PHP 5.3
 
Resource-Oriented Web Services
Resource-Oriented Web ServicesResource-Oriented Web Services
Resource-Oriented Web Services
 
Zend Framework Quick Start Walkthrough
Zend Framework Quick Start WalkthroughZend Framework Quick Start Walkthrough
Zend Framework Quick Start Walkthrough
 
Burlington, VT PHP Users Group Subversion Presentation
Burlington, VT PHP Users Group Subversion PresentationBurlington, VT PHP Users Group Subversion Presentation
Burlington, VT PHP Users Group Subversion Presentation
 

Recently uploaded

Why Teams call analytics are critical to your entire business
Why Teams call analytics are critical to your entire businessWhy Teams call analytics are critical to your entire business
Why Teams call analytics are critical to your entire business
panagenda
 
Modular Monolith - a Practical Alternative to Microservices @ Devoxx UK 2024
Modular Monolith - a Practical Alternative to Microservices @ Devoxx UK 2024Modular Monolith - a Practical Alternative to Microservices @ Devoxx UK 2024
Modular Monolith - a Practical Alternative to Microservices @ Devoxx UK 2024
Victor Rentea
 

Recently uploaded (20)

Spring Boot vs Quarkus the ultimate battle - DevoxxUK
Spring Boot vs Quarkus the ultimate battle - DevoxxUKSpring Boot vs Quarkus the ultimate battle - DevoxxUK
Spring Boot vs Quarkus the ultimate battle - DevoxxUK
 
2024: Domino Containers - The Next Step. News from the Domino Container commu...
2024: Domino Containers - The Next Step. News from the Domino Container commu...2024: Domino Containers - The Next Step. News from the Domino Container commu...
2024: Domino Containers - The Next Step. News from the Domino Container commu...
 
Why Teams call analytics are critical to your entire business
Why Teams call analytics are critical to your entire businessWhy Teams call analytics are critical to your entire business
Why Teams call analytics are critical to your entire business
 
ICT role in 21st century education and its challenges
ICT role in 21st century education and its challengesICT role in 21st century education and its challenges
ICT role in 21st century education and its challenges
 
Corporate and higher education May webinar.pptx
Corporate and higher education May webinar.pptxCorporate and higher education May webinar.pptx
Corporate and higher education May webinar.pptx
 
Strategies for Landing an Oracle DBA Job as a Fresher
Strategies for Landing an Oracle DBA Job as a FresherStrategies for Landing an Oracle DBA Job as a Fresher
Strategies for Landing an Oracle DBA Job as a Fresher
 
Axa Assurance Maroc - Insurer Innovation Award 2024
Axa Assurance Maroc - Insurer Innovation Award 2024Axa Assurance Maroc - Insurer Innovation Award 2024
Axa Assurance Maroc - Insurer Innovation Award 2024
 
Apidays New York 2024 - The value of a flexible API Management solution for O...
Apidays New York 2024 - The value of a flexible API Management solution for O...Apidays New York 2024 - The value of a flexible API Management solution for O...
Apidays New York 2024 - The value of a flexible API Management solution for O...
 
Connector Corner: Accelerate revenue generation using UiPath API-centric busi...
Connector Corner: Accelerate revenue generation using UiPath API-centric busi...Connector Corner: Accelerate revenue generation using UiPath API-centric busi...
Connector Corner: Accelerate revenue generation using UiPath API-centric busi...
 
Apidays New York 2024 - APIs in 2030: The Risk of Technological Sleepwalk by ...
Apidays New York 2024 - APIs in 2030: The Risk of Technological Sleepwalk by ...Apidays New York 2024 - APIs in 2030: The Risk of Technological Sleepwalk by ...
Apidays New York 2024 - APIs in 2030: The Risk of Technological Sleepwalk by ...
 
Artificial Intelligence Chap.5 : Uncertainty
Artificial Intelligence Chap.5 : UncertaintyArtificial Intelligence Chap.5 : Uncertainty
Artificial Intelligence Chap.5 : Uncertainty
 
AXA XL - Insurer Innovation Award Americas 2024
AXA XL - Insurer Innovation Award Americas 2024AXA XL - Insurer Innovation Award Americas 2024
AXA XL - Insurer Innovation Award Americas 2024
 
Navigating the Deluge_ Dubai Floods and the Resilience of Dubai International...
Navigating the Deluge_ Dubai Floods and the Resilience of Dubai International...Navigating the Deluge_ Dubai Floods and the Resilience of Dubai International...
Navigating the Deluge_ Dubai Floods and the Resilience of Dubai International...
 
Biography Of Angeliki Cooney | Senior Vice President Life Sciences | Albany, ...
Biography Of Angeliki Cooney | Senior Vice President Life Sciences | Albany, ...Biography Of Angeliki Cooney | Senior Vice President Life Sciences | Albany, ...
Biography Of Angeliki Cooney | Senior Vice President Life Sciences | Albany, ...
 
Repurposing LNG terminals for Hydrogen Ammonia: Feasibility and Cost Saving
Repurposing LNG terminals for Hydrogen Ammonia: Feasibility and Cost SavingRepurposing LNG terminals for Hydrogen Ammonia: Feasibility and Cost Saving
Repurposing LNG terminals for Hydrogen Ammonia: Feasibility and Cost Saving
 
Boost Fertility New Invention Ups Success Rates.pdf
Boost Fertility New Invention Ups Success Rates.pdfBoost Fertility New Invention Ups Success Rates.pdf
Boost Fertility New Invention Ups Success Rates.pdf
 
Ransomware_Q4_2023. The report. [EN].pdf
Ransomware_Q4_2023. The report. [EN].pdfRansomware_Q4_2023. The report. [EN].pdf
Ransomware_Q4_2023. The report. [EN].pdf
 
Modular Monolith - a Practical Alternative to Microservices @ Devoxx UK 2024
Modular Monolith - a Practical Alternative to Microservices @ Devoxx UK 2024Modular Monolith - a Practical Alternative to Microservices @ Devoxx UK 2024
Modular Monolith - a Practical Alternative to Microservices @ Devoxx UK 2024
 
Cyberprint. Dark Pink Apt Group [EN].pdf
Cyberprint. Dark Pink Apt Group [EN].pdfCyberprint. Dark Pink Apt Group [EN].pdf
Cyberprint. Dark Pink Apt Group [EN].pdf
 
Web Form Automation for Bonterra Impact Management (fka Social Solutions Apri...
Web Form Automation for Bonterra Impact Management (fka Social Solutions Apri...Web Form Automation for Bonterra Impact Management (fka Social Solutions Apri...
Web Form Automation for Bonterra Impact Management (fka Social Solutions Apri...
 

Introduction to PHP