Intro to PHP
Cole Geissinger
@colegeissinger
www.colegeissinger.com
Cole Geissinger
Co-Organizer @ WIMP
Front/Back-end Web Developer
Photographer
Musician
What is PHP?
An HTML-embedded server-side scripting language.
How?
Processed by a web server with the PHP processor installed.
Mr. Popularity.
Installed on more than 2.1 million web servers and over 244 million websites rely on this language.
That's about 80.5% of all server-side
languages used by websites.
Who? What? When? Where?
Created by Rasmus Lerdorf in 1995 but has since been taken over by "The PHP Group".
But What Does It Mean??
1995 - Personal Home Page
1997 - PHP: Hypertext Preprocessor
Now Things Get Complicated...
Make A Basic PHP Script
<div class="my-content">
<p>I'll put some pretty little text here...</p>
<?php PHP CODE GOES HERE ?>
</div>
Anything between the PHP tags will be treated by the web server as PHP code. Anything outside of these tags, is assumed to be HTML.
Change the File Extension
index.html -> index.php
Send Data to the Web Browser
Two (of the most popular) functions
<?php echo(); ?>
<?php print(); ?>
Examples
Echo
<?php echo('Hello, Wimps!'); ?>
<?php echo 'Hello, Wimps!'; ?>
ResultHello, Wimps!
Print
<?php print("Hello, Wimps!"); ?>
<?php print "Hello, Wimps!"; ?>
ResultHello, Wimps!
Variables, Variables, Variables!
var·i·a·ble
Variables in Use
Create a variable called $variable with the value Mr. Bucket
<?php $variable = 'Mr. Bucket'; ?>
Now we can send the variable to the browser with echo()
We can also echo variables with static text
<?php echo "Hello, $variable"; ?>
The 31 Flavors of Variables
Type | Content | Example
Strings | Text | 'text' or "Plain"
Integer | Numbers | 8675309
Floating Point | Decimals | 3.14
Boolean | True/False Value | True or 1
Nonscalar | Multivalue | Arrays or Object
Working With Strings
Single quotes will process strings as is
<?php $variable = 'Mr. Bucket'; ?>
Double quotes will process PHP inside the string <?php $variable = "Hello, $world"; ?>
What if my string has single or
double quotes?
'Escape' the characters
<?php $string = 'What\'s up, Doc?'; ?>
<?php $show = "\"Game of thrones\", FTW"; ?>
Let's Make Some Magic
What if we have two variables we want inside another?
Use concatenation!
<?php
$city = 'Santa Rosa';
$state = 'CA';
$location = $city . $state;
?>
Working with Numbers
Just like strings but don't require quotes
We Can Even Use Math!
+-*/
<?php
$math = 10 + 20;
$math = $math1 * $math2;
$math = 20 / 10;
?>
Working with Arrays/Objects
Stores multiple values into one.
Structured in a series of key-value pairs, where one pair is an item or "element" of that array.
<?php
$array = array(
'array_key' => 'array value',
0 => 'array value'
);
$object = (object) array(
'object_key' => 'array value',
0 => 'array value'
);
?>
Break It Down!
(oooh, oooh , oh, oooooh!)
Two types - Indexed and Associative
<?php
$indexed = array(
0 => 'value here',
1 => 'another value'
);
$associative = array(
'state' => 'CA',
'city' => 'Santa Rosa'
);
?>
Print our Array/Objects to the Browser
Use square brackets to target a specific key-pair for it's value
<?php echo $indexed[0]; ?>
Resultvalue here
<?php echo $associative['state']; ?>
ResultCA
Objects work the same but with arrows
<?php echo $object->array_key; ?>
Conditionals
We can use conditionals to add logic to our PHP. This will allow us to load different sections of PHP if certain conditions are met.
<?php
if ( condition ) {
// Do something!
} elseif ( condition ) {
// Do something else!
} else {
// Our fall back if no condition is met
}
?>
Conditional Operators
Conditional | Explanation | Example
== | is equal to | $apple == $apple
!= | is NOT equal to | $apple != $orange
< | less than | $five < $ten
> | greater than | $ten > $five
<= | less than/equal to| $five <= $ten
>= | grt than/equal to | $ten <= $five
! | not | ! $wimp_meetup
&& | and | $chef && $knife
|| | or | $car || $bus
XOR | and not | $ball XOR $baseball
Operator Functions
Here are some common functions used to check in our conditionals
empty() - If variable is empty
isset() - If a variable has been defined
is_int() - If a variable is a number
is_array() - If a variable is an array
is_string() - If a variable is a string
Putting it all together
Our example cannot fit in this presentation. So we'll use the power of Gist on GitHub to do just that :)
Set Forth and Code!
Suggested reading to further your new found love - PHP
PHP for the Web - Peachpit Press, 2011
Programming PHP - O'Reilly Media, 2013
PHP - Codeacedmy, Online
The Best Way to Learn PHP - Nettuts+, Online