BDI Logo

Introduction to PHP

Class 1

Welcome!

Girl Develop It is here to provide affordable and accessible programs to learn software through mentorship and hands-on instruction.


Some "rules"

  • We are here for you!
  • Every question is important
  • Help each other
  • Have fun

Welcome!

Tell us about yourself.

  • Who are you?
  • What do you hope to get out of the class?
  • What's your favorite breakfast food?

PHP is a server-side language

Laptop and server connected via the internet

Photo credits: Andrew E. Larson and John Seb Barber cc

PHP can talk to databases

Two elephants interacting

Photo credit: Ginable cc

PHP lets you reuse code

Desert landscape

Photo credit: Phil Synder cc

What is PHP?

  • Originally, PHP stood for Personal Home Page. The acronym doesn't mean much anymore, but officially it is called PHP: Hypertext Processor
  • PHP is a server-side processing language. The server reads the PHP code and outputs browser-friendly HTML
  • PHP has simple support for databases like MySQL.
  • With PHP, you can write code once and use it everywhere. Remember, you want DRY code (Don't Repeat Yourself).
  • PHP lets you build dynamic webpages that respond to input from users.

Let's Develop It

Make your first page! We are going to start in the browser and do an full install later.

Go to PHP Fiddle. Paste this code into your window and select run.

<!DOCTYPE html>
<html>
<head>
<title>Test Page</title>
</head>
<body>
<p>
  <?php
  echo 'Hello World';
  ?>
</p>
</body>
</html>

Computers need simple, clear instructions

Confused robot

Photo credit: baboon cc

Thinking like a programmer

Computers are great at processing. They are bad at understanding.

When you write a program, you must break down every step into simple pieces.

Example: Draw a Square

  1. Find a whiteboard and a dry erase marker
  2. Uncap the dry erase maker
  3. Hold the marker in your hand
  4. Place the marker against the whiteboard
  5. Move your hand 1 foot to the right
  6. Stop
  7. Move your hand 1 foot down...

Example: Make a Sandwich

Source: Harvard Students Making Sandwich: CS 50 Algorithm Intro

Basically, computers can be hard to work with!

Toddler using old word processor

Photo credit: Dan Hatton cc

So what's up with PHP?

Gears

Photo credit: Adam Foster cc

How does PHP work?

  1. A user visits a webpage written in PHP. Their browser requests the page.
  2. The server reads the code line-by-line. It may look at many different files or get information from a database.
  3. The server creates an HTML page based on the instructions in the PHP file.
  4. The server sends an HTML page back to the browser.
  5. The browser reads the code and displays a webpage.

How do people develop with PHP

Lots of different ways! Here is a common setup.

  1. A developer will set up a local environment on their own computer. This means they are using their computer as the server and the client. No one else can access these pages.
  2. When the code is ready, the developer will upload the code to a test server. This is a web server that shares the code via the internet, so the developer and others can test it.
  3. Once the code has been tested, the developer will move the code to a public web server and share it with the world.

Watch Out!

There are lots of different versions of PHP and possible configuration settings on the server. If you are using a web server to publish a page, you may need to adjust your local settings to match the server settings.

PHP Tags

You can mix PHP and HTML. When a PHP server looks at a page, it only pays attention to code within the PHP tags.

<?php CODE GOES HERE  ?>

Separating Instructions

After each individual statement, you must add a semicolon.

echo 'Hello World!';
echo 'I am glad to meet you';
echo 'I am fuzzy';

Comments

You can leave comments in your code—notes that people can read and computers will ignore.

/*I can wrap long comments
with multiple lines
like this*/
echo 'Hello World!'; //Or mark short comments like this

Getting results onto your screen

Use the command echo. If you are outputting a word, wrap it in quotes. You can use single quotes or double quotes; we'll explain the difference later.

echo 'Hello World!';

You can mix HTML tags in with your PHP

echo '

Hello World!

';

Let's Develop It

  • Go back to your test page. Add a comment to the code.
  • Try echoing some words onto the page.

Variables

A variable is a place to store values

Empty glass

Photo credit: giulia gasparro cc

Variable Values

  • When you first create a variable, it does not have a value (it is null).
  • You can set a value for a variable.
  • Variables can hold different types of information, like words, numbers, and collections of data.
  • The value of a variable can change over time.

Naming Variables

  • In PHP, you write a variable with a dollar sign followed by the name of the variable.
  • The variable name is case-sensitive.
  • A new variable needs to have a unique name.
  • Variable names need to start with a letter or underscore.
  • Variable names can only be made of letters and numbers.

Creating a Variable

To create a variable, just type a dollar sign and the variable name. PHP will create the variable for you.

$numberOfKittens;

It is a good idea to give your variable a starting value. This is called initializing the variable.

$numberOfKittens = 5;

Using a Variable

Once you have created a variable, you can use it in your code. Just type a dollar sign and the name of the variable

$numberOfKittens = 5;
echo $numberOfKittens;

Let's Develop It

In your PHP file, create a variable and give it a valid name and a value. Then, echo it out on the screen.

Numbers!

Basket of kittens

Photo credit: WJ van den Eijkhof cc

Numbers

Variable can be numbers, either integers or floats (decimals).

$numberOfKittens = 5;
$cutenessRating = 9.6;

PHP will automatically convert integers to floats if needed

Arithmetic Operators

Once you have numbers, you can do math with them!

$numberOfKittens = 5;
$numberOfPuppies = 4;
$numberOfAnimals = $numberOfKittens + $numberOfPuppies;

Arithmetic Operators

Example Name Result
-$a Negation Opposite of $a.
$a + $b Addition Sum of $a and $b.
$a - $b Subtraction Difference of $a and $b.
$a * $b Multiplication Product of $a and $b.
$a / $b Division Quotient of $a and $b.
$a % $b Modulus Remainder of $a divided by $b.

Let's Develop It

Create two variables and try some arithmetic operators. Don't forget to echo your results!

Strings

Variable can be strings, groups of characters. You put your string in quotes.

$kittensName = 'Fluffy';

If you want to use a quote in your string, you'll need to "escape" it with a backslash.

echo 'I\'d like to use an apostrophe';

Single Quotes vs. Double Quotes

  • You can use single quotes or double quotes with strings. PHP will process them differently.
  • Single-quoted strings display things "as is." PHP won't check for things like variables
  • Double-quoted strings are parsed. PHP will look for things like variables inside the string, and display them like regular code.
  • Single-quoted strings process slightly faster, but the difference is small.

Single Quotes vs. Double Quotes

Here's an example:

$numberOfCats = 5;
//outputs I have $numberOfCats cats
echo '

I have $numberOfCats cats

'; //outputs I have 5 cats echo "

I have $numberOfCats cats

";

Single Quotes vs. Double Quotes

If you want to use a variable name as part of another word, you can wrap it in curly braces

$fruit = 'apple';
//outputs We drank some apple juice
echo "

We drank some $fruit juice.

"; //outputs error message and We drank some juice made from . echo "

We drank some juice made from $fruits.

"; //outputs We drank some juice made from apples. echo "

We drank some juice made from ${fruit}s.

";

Playing with Strings

Cat playing with string

Photo credit: Mike Lawson cc

String Operators

You can put strings together with a period, the concatenation operator.

$kittensName = 'Fluffy';
$fullName = $kittensName . ' McDougle';
echo $fullName; //Outputs 'Fluffy McDougle'

String Operators

You can also use .= to add things to the end of a string.

$kittensName = 'Admiral';
$kittensName .= ' Snuggles';
echo $kittensName; //Outputs 'Admiral Snuggles'

Concatenate!

Cat jumping on another cat.

Photo credit: Matt cc

Let's Develop It

Create two variables, a first name and a last name, and then put them together to make a full name. Don't forget to echo your results!

Combining strings and numbers

You can use the concatenate function to mix strings and numbers. When you do this, PHP will treat the number like a string.

$numberOfFruit = 6;
$typeOfFruit = 'bananas';
$allTheFruit = 'Wow, I have ' . $numberOfFruit . ' ' . $typeOfFruit . '!';
echo $allTheFruit;

You can also use double quotes to help combine the variables

$numberOfFruit = 6;
$typeOfFruit = 'bananas';
$allTheFruit = "Wow, I have $numberOfFruit $typeOfFruit!";
echo $allTheFruit;

Let's Develop It

Create a program to calculate the tip at a restuarant. It should:

  • Have variables for the bill pre-tip and the tip percentage
  • Calculate the total bill
  • Output a sentence like "Your total bill, with tip, is $14.75"

You did it!

People celebrating

Photo credit: Mircea cc

Resources

  • PHP Manual, the official PHP documentation. Check the comments; they are useful.
  • Code Academy, with interactive PHP lessons to help you review.