Getting Started
with PHP
MemphisPHP.org
Joe Ferguson - joe@joeferguson.me
Professionally
● Web Developer at RocketFuel
● PHP / LAMP Focused
Semi Professional
● Co-Organizer for MemphisPHP.org
● MidsouthMakers - Hackerspace Leader
● HACKmemphis.com Organizer
Who is this guy?
It’s a recursive anagram…
PHP: Hypertext Preprocessor is a widely used, general-
purpose scripting language that was originally designed
for web development to produce dynamic web pages
What is PHP?
● Widespread Availability
○ PHP is now installed on more than 244 million
websites and 2.1 million web servers (Source:
Wikipedia)
● Ease of use.
○ Extremely simple for newcomers
○ Advanced features for professional programmers
○ Looslely typed
● Great Community Support
● Many corporations throwing support
behind PHP projects
Why use PHP?
Who uses it? What runs on PHP?
...and many, MANY others
<html>
<head>
<title>Example 1 - Getting Started with
PHP<</title>
</head>
<body>
<?php echo '<p>Hello World</p>'; ?>
</body>
</html>
Jumping into PHP - Example 1
You can add any content to a PHP file.
Anything not between PHP tags is ignored by
PHP.
Using echo, you can print from PHP. As
example 1 shows, even PHP.
Example 1 - Basic HTML + PHP
PHP is very easy to pick up and run with.
This is one of the main reasons it is so
widespread.
Looks easy right?
$_GET Variables come from the URL
Bad:
<?php
$name = $_GET['name'];
?>
NEVER trust data from your users!
Good:
<?php
if(isset($_GET['name'])){
$name = filter_var($_GET['name'],FILTER_SANITIZE_STRING);
}
?>
Getting Data from the User
So what is happening?
● First we check if the name exists by
looking at $_GET[‘name’]
○ if(isset($_GET['name'])){
● If it does exist, we pass it to a function to
sanitize the input and set the results equal
to $name
○ $name = filter_var($_GET['name'],FILTER_SANITIZE_STRING);
Getting Data from the User
● isset
● empty
● filter_var
● arrays
● strings
● omgwtf!
The best PHP resources:
● phptherightway.com
● php.net
I can’t keep track of all this!
PHP Specification Request (PSR)
The Framework Interop Group:
● PSR-0
● PSR-1
● PSR-2
Other Styles:
● PEAR Coding Standards
● Zend Coding Standards
OMG your { are on the wrong line!
There are subtle differences among the PSR
coding styles approved by the FIG.
If you’re wanting to maximize your
reusability: pick a PSR similar to the code
you are working with.
Most projects won’t take code if it doesn’t
match the style they have adopted.
Why so many? What do I use?
Strings
● echo ‘PHP is my favorite language’;
● $variable = ‘PHP is amazing’;
● echo "PHP! you'll be astonished";
● echo 'PHP! you'll be astonished';
Concatenation
● echo "Hello, " . $prefix . " " . $name;
Single quote strings do not get parsed.
Double quote strings get parsed. Are slower
than single quote strings because of this.
Before we get too complicated:
Simple Arrays:
<?php
$fruit = array();
$fruit[] = 'apple';
$fruit[] = 'pear';
$fruit[] = 'orange';
$fruit[] = 'lemon';
$fruit[] = 'pineapple';
?>
Arrays - associating values to keys
$food = array();
$food['fruit'] = array();
$food['fruit'][] = 'apple';
$food['fruit'][] = 'pear';
$food['fruit'][] = 'orange';
$food['fruit'][] = 'lemon';
$food['fruit'][] = 'pineapple';
$food['meat'] = array();
$food['meat'][] = 'bacon';
$food['meat'][] = 'steak';
$food['meat'][] = 'chicken';
$food['meat'][] = 'fish';
Multidimensional Arrays - Ex 5
● We know that PHP is awesome
● We know A LOT of people use PHP
● We’ve greeted the world of PHP
● We have seen how to get data from users
● We are absolute EXPERTS at Arrays
The Story So Far
Conditions
$name = 'Joe';
//$name = 'Bill the pony';
if($name == 'Joe'){
echo '<p>Hello Joe, welcome back</p>';
} else {
echo '<p>Hello ' . $name . '</p>';
}
Putting it together… Example 6
Foreach steps through each item.
You perform some action on each item.
foreach($fruit AS $key => $value){
echo '<li>' . $value . '</p>';
}
For Loops - Example 7
While loops perform the action while a
condition is met and then stops.
$fruit_count = count($fruit);
$f = 0;
while ($f < $fruit_count){
echo '<li>' . $fruit[$f] . '</li>';
$f++;
}
While Loops - Example 8
function getFruit(){
$fruit = array();
$fruit[] = 'apple';
$fruit[] = 'pear';
$fruit[] = 'orange';
$fruit[] = 'lemon';
$fruit[] = 'pineapple';
return $fruit;
}
FUNctions - Example 9
Using the previous getFruit function:
$fruit = getFruit();
$fruit is now an array created by the function
Using Functions - Example 9
● You can pass data into an array by adding
arguments
function getFruitOrMeat($fruit_or_meat){
…
}
$fruit_or_meat is data we are passing into
the getFruitOrMeat function
Function Arguments Example 10
The best PHP resources:
● phptherightway.com
● php.net
Online Learning:
● codecademy.com
● php.net/manual/en/getting-started.php
More Information

Getting started with php

  • 1.
  • 2.
    Joe Ferguson -joe@joeferguson.me Professionally ● Web Developer at RocketFuel ● PHP / LAMP Focused Semi Professional ● Co-Organizer for MemphisPHP.org ● MidsouthMakers - Hackerspace Leader ● HACKmemphis.com Organizer Who is this guy?
  • 3.
    It’s a recursiveanagram… PHP: Hypertext Preprocessor is a widely used, general- purpose scripting language that was originally designed for web development to produce dynamic web pages What is PHP?
  • 4.
    ● Widespread Availability ○PHP is now installed on more than 244 million websites and 2.1 million web servers (Source: Wikipedia) ● Ease of use. ○ Extremely simple for newcomers ○ Advanced features for professional programmers ○ Looslely typed ● Great Community Support ● Many corporations throwing support behind PHP projects Why use PHP?
  • 5.
    Who uses it?What runs on PHP? ...and many, MANY others
  • 6.
    <html> <head> <title>Example 1 -Getting Started with PHP<</title> </head> <body> <?php echo '<p>Hello World</p>'; ?> </body> </html> Jumping into PHP - Example 1
  • 7.
    You can addany content to a PHP file. Anything not between PHP tags is ignored by PHP. Using echo, you can print from PHP. As example 1 shows, even PHP. Example 1 - Basic HTML + PHP
  • 8.
    PHP is veryeasy to pick up and run with. This is one of the main reasons it is so widespread. Looks easy right?
  • 9.
    $_GET Variables comefrom the URL Bad: <?php $name = $_GET['name']; ?> NEVER trust data from your users! Good: <?php if(isset($_GET['name'])){ $name = filter_var($_GET['name'],FILTER_SANITIZE_STRING); } ?> Getting Data from the User
  • 10.
    So what ishappening? ● First we check if the name exists by looking at $_GET[‘name’] ○ if(isset($_GET['name'])){ ● If it does exist, we pass it to a function to sanitize the input and set the results equal to $name ○ $name = filter_var($_GET['name'],FILTER_SANITIZE_STRING); Getting Data from the User
  • 11.
    ● isset ● empty ●filter_var ● arrays ● strings ● omgwtf! The best PHP resources: ● phptherightway.com ● php.net I can’t keep track of all this!
  • 12.
    PHP Specification Request(PSR) The Framework Interop Group: ● PSR-0 ● PSR-1 ● PSR-2 Other Styles: ● PEAR Coding Standards ● Zend Coding Standards OMG your { are on the wrong line!
  • 13.
    There are subtledifferences among the PSR coding styles approved by the FIG. If you’re wanting to maximize your reusability: pick a PSR similar to the code you are working with. Most projects won’t take code if it doesn’t match the style they have adopted. Why so many? What do I use?
  • 14.
    Strings ● echo ‘PHPis my favorite language’; ● $variable = ‘PHP is amazing’; ● echo "PHP! you'll be astonished"; ● echo 'PHP! you'll be astonished'; Concatenation ● echo "Hello, " . $prefix . " " . $name; Single quote strings do not get parsed. Double quote strings get parsed. Are slower than single quote strings because of this. Before we get too complicated:
  • 15.
    Simple Arrays: <?php $fruit =array(); $fruit[] = 'apple'; $fruit[] = 'pear'; $fruit[] = 'orange'; $fruit[] = 'lemon'; $fruit[] = 'pineapple'; ?> Arrays - associating values to keys
  • 16.
    $food = array(); $food['fruit']= array(); $food['fruit'][] = 'apple'; $food['fruit'][] = 'pear'; $food['fruit'][] = 'orange'; $food['fruit'][] = 'lemon'; $food['fruit'][] = 'pineapple'; $food['meat'] = array(); $food['meat'][] = 'bacon'; $food['meat'][] = 'steak'; $food['meat'][] = 'chicken'; $food['meat'][] = 'fish'; Multidimensional Arrays - Ex 5
  • 17.
    ● We knowthat PHP is awesome ● We know A LOT of people use PHP ● We’ve greeted the world of PHP ● We have seen how to get data from users ● We are absolute EXPERTS at Arrays The Story So Far
  • 18.
    Conditions $name = 'Joe'; //$name= 'Bill the pony'; if($name == 'Joe'){ echo '<p>Hello Joe, welcome back</p>'; } else { echo '<p>Hello ' . $name . '</p>'; } Putting it together… Example 6
  • 19.
    Foreach steps througheach item. You perform some action on each item. foreach($fruit AS $key => $value){ echo '<li>' . $value . '</p>'; } For Loops - Example 7
  • 20.
    While loops performthe action while a condition is met and then stops. $fruit_count = count($fruit); $f = 0; while ($f < $fruit_count){ echo '<li>' . $fruit[$f] . '</li>'; $f++; } While Loops - Example 8
  • 21.
    function getFruit(){ $fruit =array(); $fruit[] = 'apple'; $fruit[] = 'pear'; $fruit[] = 'orange'; $fruit[] = 'lemon'; $fruit[] = 'pineapple'; return $fruit; } FUNctions - Example 9
  • 22.
    Using the previousgetFruit function: $fruit = getFruit(); $fruit is now an array created by the function Using Functions - Example 9
  • 23.
    ● You canpass data into an array by adding arguments function getFruitOrMeat($fruit_or_meat){ … } $fruit_or_meat is data we are passing into the getFruitOrMeat function Function Arguments Example 10
  • 24.
    The best PHPresources: ● phptherightway.com ● php.net Online Learning: ● codecademy.com ● php.net/manual/en/getting-started.php More Information