Girl Develop It is here to provide affordable and accessible programs to learn software through mentorship and hands-on instruction.
Some "rules"
Tell us about yourself.
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/
Make your first page! Find your www directory. On Windows, the default is C:\wamp\www. On a Mac, it is \Applications\MAMP\www\. 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>
Photo credits: Andrew E. Larson and John Seb Barber cc
Photo credit: Phil Synder cc
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
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';
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!
Functions are separable, reusable pieces of code.
Photo credit: Rick Payette cc
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();
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
addOne($numberOfKittens);
addOne($numberOfPuppies);
Turn the code you wrote to output someone's full name into a function, then use it.
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.
Add a return statement to your name function. Use that function to set the value of a variable.