2. Who are you? FooLab
• Have you ever written PHP before?
• Have you ever written computer code before?
• Have you ever seen computer code?
• Ask for the name of your neighbor on each side.
2
3. Anna Filina FooLab
• PHP Quebec - user group, organizer.
• ConFoo - non for profit Web conference, organizer.
• FooLab Inc. - IT consulting, vice-president.
• I write code.
• I train people.
3
4. Programming FooLab
Describing procedures
Input
Find Password
username
username matches?
and password
Yes No
Open Display error
account page message
4
5. Functions FooLab
One step in the whole program
pi 3.141592 ...
26, 58 max 58
Y-m-d date 2012-04-26
5
6. Interactive Shell FooLab
• We can see result of PHP code as we type it.
• Open your console. Type phpsh
• After each line of code, press enter to execute it.
6
8. Interpreter FooLab
• I like like
php> echo echo;
• I like the word “like”
php> echo "echo";
8
9. Data Types FooLab
• Integer, can’t confuse with commands or functions:
php> echo 33;
• String, use quotes:
php> echo "Programming is cool";
• There are more types, but that’s for later.
9
10. Variables FooLab
• "Programming is cool"
• "Design is cool"
• "Video editing is cool"
php> $hobby = "Design";
php> echo $hobby;
php> echo "$hobby is cool";
10
11. FooLab
Design
• Use the $ sign to refer
to the bin’s name.
hobby
• Use the = sign to put
content in the bin.
$hobby = "Design"
11
12. FooLab
Design
• No sign is needed to get
the bin’s content out.
hobby
• A variable is where we
put a value.
echo $hobby
12
13. Writing Code in Files FooLab
• It’s easier to write multiple lines of code in a file.
• Open your text editor.
• Open the file /var/www/php1/script.php
13
14. FooLab
• Quit the interactive shell by typing:
php> q
• Now you can run your file using:
$ php /var/www/php1/script.php
• Repeat the previous command by pressing the up arrow.
• Every time we edit our file, we’ll test the code.
14
15. FooLab
• The file currently contains the following text:
<?php
$hobby = "Design";
echo "$hobby is cool";
?>
• The file ends in empty lines. Don’t delete them.
15
16. Movie Price FooLab
• Movie costs 12$
• Popcorn costs 8$
• Popcorn can be shared between two people.
16
17. FooLab
• What happens with popcorn when we have an odd
number of people?
ceil(3 / 2);
17
18. Procedure FooLab
Calculate total movie cost based on number of people
Set number of Get number of
people popcorns Popcorns * 8
(variable) required
Display sum Tickets * 12
18
19. Practice! FooLab
• Write a script that, given any number of people,
calculates the total price for movie and popcorn.
• Set number of people (variable)
• Get number of popcorns required
• Popcorns * 8
• Tickets * 12
• Display sum
19
26. String and Math Functions FooLab
• Open the interactive shell: phpsh
php> echo rand(1, 3);
php> echo rand(1, 100);
php> echo strlen("FooLab");
php> echo substr("FooLab", 3);
php> echo substr("FooLab", 3, 1);
26
27. Position zero FooLab
• Many programming languages start counting at zero.
F o o L a b
advance by: 0 1 2 3 4 5
27
28. Practice! FooLab
• Write a function that calculates a rectangle’s area using
height and width.
• Write a function that returns a random letter from the
alphabet. Hint: $letters = “abcdefg...”
• substr(text, start, length)
• rand(min, max)
28
39. Writing Loops FooLab
• for (initially ; iterate this time? ; after each iteration)
for ($i = 1; $i <= 3; $i++) {
echo $i;
}
• $i++ is the same as $i = $i + 1
39
41. Chorus FooLab
for ($i = 1; $i <= 4; $i++) {
echo "This song is just six words longn";
}
41
42. Array FooLab
An ordered set of related elements
42
43. Array FooLab
An ordered set of related elements
43
44. What Is An Array? FooLab
page page page
book
0 1 2
number number "Text"
• You can put books in boxes for “nested” arrays,
but that’s for another day.
44
45. Acces Elements By Index FooLab
$movies = array("Titanic", "Shrek", "Die Hard");
echo $movies[1];
45
46. Iterating Over Arrays FooLab
$movies = array("Titanic", "Shrek", "Die Hard");
foreach ($movies as $movie) {
echo "I watched $movien";
}
46
47. Getting Index and Value FooLab
• In addition to the value, you can also get the index for
each iteration:
foreach ($movies as $index => $movie)
47
48. Concatenation FooLab
• Link bits of text together using a dot (.)
echo "You rolled " . rand(2, 12);
• Useful in a loop
$sequence = "";
for ($i = 1; $i <= 10; $i++) {
$sequence = $sequence . rand(0, 9);
}
echo $sequence;
48
49. Practice! FooLab
• Write a function that creates a random 9-character,
pronouncable password.
• 3 cyllables, 3 letters each
• Consonant, vowel, consonant
• Should produce something like this:
“hagrokwag”
49
51. Trivia FooLab
• 6 ^ 21 + 3 ^ 5
gives over 20 quadrillions combinations
• It will take millions of years for a computer
to try them all
• And you can pronounce it, making it easy to memorize!
51
52. Next Steps FooLab
• Go to phpjunkyard.com
• Download some script
• See how it works
• Play with the code
• Anything you put in /var/www/php1 can be accessed in
the browser: http://php1.local/
52
53. Resources FooLab
• php.net has a manual and a reference for all functions.
• phpquebec.org is the PHP users group in Montreal.
53