Girl Develop It is here to provide affordable and accessible programs to learn software through mentorship and hands-on instruction.
Photo credits: Andrew E. Larson and John Seb Barber cc
Photo credit: Phil Synder cc
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 are great at processing. They are bad at understanding.
When you write a program, you must break down every step into simple pieces.
Source: Harvard Students Making Sandwich: CS 50 Algorithm Intro
Photo credit: Dan Hatton cc
Photo credit: Adam Foster cc
Lots of different ways! Here is a common setup.
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.
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 ?>
After each individual statement, you must add a semicolon.
echo 'Hello World!';
echo 'I am glad to meet you';
echo 'I am fuzzy';
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
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!
';
A variable is a place to store values
Photo credit: giulia gasparro cc
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;
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;
In your PHP file, create a variable and give it a valid name and a value. Then, echo it out on the screen.
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
Once you have numbers, you can do math with them!
$numberOfKittens = 5;
$numberOfPuppies = 4;
$numberOfAnimals = $numberOfKittens + $numberOfPuppies;
| 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. |
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';
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
";
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.
";
Photo credit: Mike Lawson cc
You can put strings together with a period, the concatenation operator.
$kittensName = 'Fluffy';
$fullName = $kittensName . ' McDougle';
echo $fullName; //Outputs 'Fluffy McDougle'
You can also use .= to add things to the end of a string.
$kittensName = 'Admiral';
$kittensName .= ' Snuggles';
echo $kittensName; //Outputs 'Admiral Snuggles'
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!
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;
Create a program to calculate the tip at a restuarant. It should: