PHP 101

By Margaret May

Organized by Elena Levine

Partially based on a GDI class created by Sylvia Richardson


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

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


Tell us about yourself!

  • Who are you?
  • What do you hope to get out of the class?
  • What experience do you have with PHP? Remember - NO experience is totally fine!!
  • What is your favorite 80's song?

What we will cover today

  • Intro to Programming
  • Compile vs. Interpreted Languages
  • Client-side vs. Server-side Languages
  • Actually start coding...
  • ... and learning some syntax

A Quick Aside

Let's get wifi set up

Start downloading XAMPP from: here

----- Intro to Programming ------

What is Programming?

A way of telling the computer what to do. Commands!

Types of Programming

----- Compiled vs. Interpreted Languages -----

Breaking down High-Level Languages

Compiled Languages

Source code is translated to machine code before it can run. Usually these are lower-level languages closer to machine code.

Interpreted Languages

Code is executed automatically. Each statement is translated into subroutines that are already compiled into machine code. These are higher-level languages abstracted from machine code.

----- Client-Side vs. Server-Side Languages -----

Client-Side Languages

All processing takes place in the user's browser

Server-Side Languages

The server reads the PHP code and outputs browser-friendly HTML.

Web Servers

  • A conversation
  • Terminology: resource, user agent, HTTP, path
  • Path translation
    GET /path/file.html HTTP/1.1

Soooooo.... where does PHP fall in all of this?

Compiled or interpreted?

Client-side or server-side?

You might be asking... why do we need to know this type of information?

Because it's critical to know this type of information to truly understand PHP.

"A Page Loading Play"

Compiled vs. Interpreted Languages: How They Differ


  • is an interpreted language!
  • is a server-side processing language!
  • has simple support for databases like MySQL.
  • lets you build dynamic web pages that respond to input from users.
  • allows you to write code once and use it everywhere. Remember, you want DRY code (Don't Repeat Yourself).

History of PHP

  • Before:
    • Rasmus Lerdorf originally developed PHP to attract people to his online resume
    • Personal Home Page Tools
    • PHP Tools
    • FI (Forms Interpreter) - awkward, teenage
    • PHP/FI
    • PHP (Hypertext Preprocessor)
  • Now:
Break... and when we get back let's start coding!

Let's Develop It

Let's get PHP running on your machine.

Get XAMPP from: here

"Install" XAMPP

Test: http://localhost/

Let's Develop It

Make your first page! Find your htdocs directory. It should be something like: C:\xampp\htdocs. Create a folder called class1, and then create a file inside called hello-world.php.

<!DOCTYPE html>
		<title>Test Page</title>
			echo 'Hello World';

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';

This and all following slides:


A variable is a place to store values.

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.


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.


Cat jumping on another cat.

Photo credit: WJ van den Eijkhof cc


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!


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';

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'


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!


Functions are separable, reusable pieces of code. Pile of legos

Photo credit: Rick Payette cc

Using Functions

First, declare the function.

			function turtleFact() {
				echo 'A turtle\'s lower shell is called plastron.';

Then, use it as many times as you want!



Functions can accept input values, called arguments.

			function callKitten ($kittenName){
				echo 'Come here, ' . $kittenName . '!';
			callKitten ('Fluffy'); //outputs 'Come here, Fluffy!'
			function addNumbers($a, $b) {
				echo $a + $b;
			addNumbers(5,7); //outputs 12
			addNumbers(9,12); //outputs 21


You can also pass variables into functions. These variables do not need to have the same name as the function arguments.

			function addOne($inputNumber){
				$newNumber = $inputNumber + 1;
				echo '<p>You now have ' . $newNumber . '</p>';
			//Declare variables
			$numberOfKittens = 5;
			$numberOfPuppies = 4;
			//Use them in functions

Let's Develop It

Turn the code you wrote to output someone's full name into a function, then use it.

Returning Values

You can have a function give you back a value, to use later.

			function square($num) {
				return $num * $num;
			echo square(4);   // outputs '16'.
			$squareOfFive = square(5); // will make $squareOfFive equal 25.

Return will immediately end a function.

Let's Develop It

Add a return statement to your name function. Use that function to set the value of a variable.

You did it!

People celebrating

Photo credit: Mircea cc

Unless otherwise stated, all images were obtained from:


Securing your XAMPP is very important, so please make sure password protect /xampp using this guide

Sections: Use XAMPP Security Script to Password Protect /xampp and phpMyAdmin root User and Impacts of your new security and testing it


Use the PHP Manual and the Internet as a Reference.

  • You will be taking a trip soon! It's a 5 day hiking trip in the Adirondacks! You will use 2 pairs of socks per day. Write a function called getTotalPairsOfSocks that will calculate how many pairs of socks you will need to pack for your trip. Your function should take 2 parameters - number of travel days and number of pairs of socks per day. Bonus! Try adding a half-day to your trip! See what happens when you run your program. Restriction: you can't have any single socks lying around. Call your file my-packing-helper.php


Use the PHP Manual and the Internet as a Reference.

  • Write a function that concatenates two Strings. Write a function that adds two numbers. Call both functions with the Strings "1" and "2" and see what happens. Call your file function-fun.php.