getting started with php

24
Getting Started with PHP MemphisPHP.org

Upload: joe-ferguson

Post on 09-Jul-2015

1.462 views

Category:

Technology


0 download

DESCRIPTION

Getting started with PHP talk given at the MemphisPHP.org User group meet up in September 2013.

TRANSCRIPT

Page 1: Getting started with php

Getting Started with PHPMemphisPHP.org

Page 2: Getting started with php

Joe Ferguson - [email protected]

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?

Page 3: Getting started with php

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?

Page 4: Getting started with 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?

Page 5: Getting started with php

Who uses it? What runs on PHP?

...and many, MANY others

Page 6: Getting started with php

<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

Page 7: Getting started with php

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

Page 8: Getting started with 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?

Page 9: Getting started with php

$_GET Variables come from the URLBad:<?php$name = $_GET['name'];?>NEVER trust data from your users!

Good:<?phpif(isset($_GET['name'])){ $name = filter_var($_GET['name'],FILTER_SANITIZE_STRING);}?>

Getting Data from the User

Page 10: Getting started with php

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

Page 11: Getting started with php

● isset● empty● filter_var● arrays● strings● omgwtf!

The best PHP resources:● phptherightway.com● php.net

I can’t keep track of all this!

Page 12: Getting started with php

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!

Page 13: Getting started with php

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?

Page 14: Getting started with php

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:

Page 15: Getting started with php

Simple Arrays:<?php$fruit = array();$fruit[] = 'apple';$fruit[] = 'pear';$fruit[] = 'orange';$fruit[] = 'lemon';$fruit[] = 'pineapple';?>

Arrays - associating values to keys

Page 16: Getting started with php

$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

Page 17: Getting started with php

● 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

Page 18: Getting started with php

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

Page 19: Getting started with php

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

Page 20: Getting started with php

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

Page 21: Getting started with php

function getFruit(){ $fruit = array(); $fruit[] = 'apple'; $fruit[] = 'pear'; $fruit[] = 'orange'; $fruit[] = 'lemon'; $fruit[] = 'pineapple'; return $fruit;}

FUNctions - Example 9

Page 22: Getting started with php

Using the previous getFruit function:

$fruit = getFruit();

$fruit is now an array created by the function

Using Functions - Example 9

Page 23: Getting started with php

● 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

Page 24: Getting started with php

The best PHP resources:● phptherightway.com● php.net

Online Learning:● codecademy.com● php.net/manual/en/getting-started.php

More Information