Girl Develop It is here to provide affordable and accessible programs to learn software through mentorship and hands-on instruction.
Some "rules"
Not seeing anything when you run your code? You may need to turn on error logging. Put this code at the top of your page.
// Report all PHP errors
error_reporting(-1);
Photo credit: Deepak Bhatia cc
In this code, spot the comments, variables, operator, function, argument, and return value.
function calculateTip($total) {
$tipPercent = 0.15; //Can be changed
return ($total * $tipPercent);
}
$billTotal = 10;
$billTip = calculateTip($billTotal);
$receipt = 'Meal: ' . $billTotal . ' Tip: ' . $billTip .
' Total: ' . ($billTotal + $billTip);
echo $receipt;
The scope of a variable is how long the computer will remember it.
A variable declared outside a function has a global scope and can only be accessed outside a function.
$awesomeGroup = 'Girl Develop It'; //Global scope
function whatIsAwesome() {
echo $awesomeGroup . ' is pretty awesome.';
}
whatIsAwesome();
A variable declared within a function has a local scope and can only be accessed within that function.
function whatIsAwesome() {
$awesomeGroup = 'Girl Develop It'; //Local scope
}
echo $awesomeGroup . ' is pretty awesome.';
Photo credit: elycefeliz cc
Boolean variables represent the logical values True and False
$catsAreBest = true;
$dogsRule = false;
If you try to use another variable as a boolean, PHP will guess. The number 0, the empty string '', and the string '0' are considered false, everything else reads as true.
Use if to tell PHP which lines of code to execute, based on a condition.
if (condition) {
// statements to execute
}
$bananas = 5
if ($bananas > 0) {
echo 'You have some bananas!';
}
Example | Name | Result |
---|---|---|
$a == $b | Equal | TRUE if $a is equal to $b (can be different types). |
$a === $b | Identical | TRUE if $a is equal to $b, and the the same type. |
$a != $b | Not equal | TRUE if $a is not equal to $b (can be different types). |
$a !== $b | Not identical | TRUE if $a is not equal to $b, or they are not the same type. |
$a < $b | Less than | TRUE if $a is strictly less than $b. |
$a > $b | Greater than | TRUE if $a is strictly greater than $b. |
$a <= $b | Less than or equal to | TRUE if $a is less than or equal to $b. |
$a >= $b | Greater than or equal to | TRUE if $a is greater than or equal to $b. |
Don't mix up = and ==
Photo credit: Eugene Zemlyanskiy cc
Make a variable called "temperature." Write some code that tells you to put on a coat if it is below 50 degrees.
Photo credit: Thomas Hawk cc
Use else to provide an alternate set of instructions.
$age = 28;
if ($age >= 16) {
echo 'Yay, you can drive!';
} else {
echo 'Sorry, but you have ' . (16 - $age) .
' years until you can drive.';
}
If you have multiple conditions, you can use else if.
$age = 20;
if ($age >= 35) {
echo 'You can vote AND hold any place in government';
} else if ($age >= 30) {
echo 'You can vote AND run for the Senate';
} else if ($age >= 18) {
echo 'You can vote';
} else {
echo 'You can\'t vote, but you can still
write your representatives.';
}
Modify your "wear a coat" code for these conditions:
Example | Name | Result |
---|---|---|
$a and $b | And | TRUE if both $a and $b are TRUE . |
$a && $b | And | TRUE if both $a and $b are TRUE . |
$a or $b | Or | TRUE if either $a or $b is TRUE . |
$a || $b | Or | TRUE if either $a or $b is TRUE . |
$a xor $b | Xor | TRUE if either $a or $b is TRUE , but not both. |
! $a | Not | TRUE if $a is not TRUE . |
You can use these operators to combine conditions.
$bananas = 5;
if ($bananas >=2 && $bananas <7) {
echo 'You have a reasonable number of bananas';
} else {
echo 'Check your banana supply';
}
Add a logical operator to your what to wear program.
Photo credit: Courtney Patubo Kranzke cc
While will repeat the same code over and over as long as some condition is met.
$bottlesOfBeer = 99;
while ($bottlesOfBeer >= 1) {
echo $bottlesOfBeer . ' bottles of beer on the wall <br />';
$bottlesOfBeer = $bottlesOfBeer - 9;
}
Make sure something changes in the loop, or your loop will go on forever...
Photo credit: Samuel John cc
For loops are very similar, but you declare a counter in the statement.
//will count 1 to 10
for ($i = 1; $i <= 10; $i++) {
echo $i;
}
You can add other statements or logical operators inside the loops.
//Count from 1 to 50.
for ($i = 1; $i <= 50; $i++) {
echo $i;
//Says 'Buzz' after multiples of three
if ($i%3 == 0) {
echo ' Buzz';
}
//Says 'Bang' after multiples of five
if ($i%5 == 0) {
echo ' Bang';
}
echo '<br />'; //line break
}
Write a loop that gives you the 9's times table,
from 9 x 1 = 9 to 9 x 12 = 108.
Finish early? Try using a loop inside a loop to write all the times tables, from 1 to 12.
Photo credit: Jesus Solana cc
Arrays are lists of values associated with a key. The key needs to be an integer or a string, the value can be any type.
$kittenColors = array(
'Fluffy' => 'tabby',
'Midnight' => 'black',
'Admiral Snuggles' => 'white'
);
Once you have an array, you use a key to call a value.
$kittenColors = array(
'Fluffy' => 'tabby',
'Midnight' => 'black',
'Admiral Snuggles' => 'white'
);
echo $kittenColors['Midnight']; //prints 'black'
Arrays don't have a fixed length; you can add and take away items.
$kittenColors = array(
'Fluffy' => 'tabby',
'Midnight' => 'black',
'Admiral Snuggles' => 'white'
);
// This adds a new element to the array with key 'Monster'
$kittenColors['Monster'] = 'gray';
// This removes the element with the key 'Midnight'
unset($kittenColors['Midnight']);
If you don't specify a key, PHP will just automatically assign integers as keys, starting with zero.
$fruits = array("apple", "orange", "banana", "kiwi");
var_dump($fruits); //shows whole array
echo $fruits[1]; //will print orange
//You can add with no key as well
$fruits[] = 'mango';
var_dump($fruits);
Create an array of your friends and their favorite foods. Echo a few values onto your screen.
Foreach is a special type of loop that works with arrays.
$numberList = array(1, 2, 3, 4, 5);
foreach ($numberList as $value) {
echo $value . ' times 2 equals ' . ($value*2) . '<br />';
}
You can also use the key values in your foreach loop.
$kittenColors = array(
'Fluffy' => 'tabby',
'Midnight' => 'black',
'Admiral Snuggles' => 'white',
);
foreach ($kittenColors as $name => $color) {
echo 'The kitten ' . $name . ' has ' . $color . ' fur.<br />';
}
Take your array of friends and write a foreach loop that echos "My friend NAME likes FOOD."
Photo credit: OnceAndFutureLaura cc