PHP 101

By Margaret May

Organized by Elena Levine

Partially based on a GDI class created by Sylvia Richardson

Welcome!

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.

https://github.com/Roenok/gdi-intro-php-mysql

Welcome!

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?

https://github.com/Roenok/gdi-intro-php-mysql

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!

https://github.com/Roenok/gdi-intro-php-mysql

Types of Programming

http://shawnbiddle.com/js101/

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

Breaking down High-Level Languages

http://shawnbiddle.com/js101/

Compiled Languages

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

http://shawnbiddle.com/js101/

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.

http://shawnbiddle.com/js101/

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

Client-Side Languages

All processing takes place in the user's browser

http://shawnbiddle.com/js101/

Server-Side Languages

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

http://shawnbiddle.com/js101/

Web Servers

  • A conversation
  • Terminology: resource, user agent, HTTP, path
  • Path translation
    
    GET /path/file.html HTTP/1.1
    Host: www.example.com
    
    
    /home/www/path/file.html
    

http://en.wikipedia.org/wiki/Web_server

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

http://shawnbiddle.com/js101/

PHP...

  • 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:
http://php.net/manual/en/history.php.php
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>
<html>
	<head>
		<title>Test Page</title>
	</head>
	<body>
		<p>
		<?php
			echo 'Hello World';
		?>
		</p>
	</body>
</html>
					

https://github.com/Roenok/gdi-intro-php-mysql

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  ?>
					

https://github.com/Roenok/gdi-intro-php-mysql

Separating Instructions

After each individual statement, you must add a semicolon.


<?php
    echo 'Hello World!';
    echo 'I am glad to meet you';
    echo 'I am fuzzy';
?>
					

This and all following slides: https://github.com/Roenok/gdi-intro-php-mysql

Variables

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.


	<?php
		$numberOfKittens;
	?>
					

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


	<?php
		$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


	<?php
		$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!

Cat jumping on another cat.

Photo credit: WJ van den Eijkhof cc

Numbers

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


		<?php
			$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!


		<?php
			$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.


		<?php
			$kittensName = 'Fluffy';
		?>
						

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


		<?php
			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.


		<?php
			$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.


		<?php
			$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!

Functions

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

Photo credit: Rick Payette cc

Using Functions

First, declare the function.


		<?php
			function turtleFact() {
				echo 'A turtle\'s lower shell is called plastron.';
			}
		?>
						

Then, use it as many times as you want!


		<?php
			turtleFact();
		?>
						

Arguments

Functions can accept input values, called arguments.


		<?php
			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
		?>
						

Arguments

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


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

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.


		<?php
			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: http://www.freeimages.com/

Homework!

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

Homework!

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

Homework!

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.