GDI Logo

Intro to PHP and MySQL

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 is your favorite 80's song?

Let's Develop It

Let's get PHP running on your machine.

PCs: http://www.wampserver.com/en/download.php

Macs: http://www.mamp.info/en/downloads/index.html

Test: http://localhost/

Let's Develop It

Make your first page! Find your www directory. On Windows, the default is C:\wamp\www. On a Mac, it is \Applications\MAMP\htdocs\. Create a new folder, called gdi, and then create a file inside called index.php.

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

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.

Computers need simple, clear instructions

Confused robot

Photo credit: baboon cc

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

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!

Cat jumping on another cat.

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

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!

Functions

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!

turtleFact();

Arguments

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

Arguments

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

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

Resources

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