• Share
  • Email
  • Embed
  • Like
  • Save
  • Private Content
Intro to PHP for Beginners
 

Intro to PHP for Beginners

on

  • 4,068 views

Anna Filina's Slides from her April 26, 2012 presentation to Montreal Girl Geeks

Anna Filina's Slides from her April 26, 2012 presentation to Montreal Girl Geeks

Statistics

Views

Total Views
4,068
Views on SlideShare
3,681
Embed Views
387

Actions

Likes
1
Downloads
19
Comments
0

3 Embeds 387

http://montrealgirlgeeks.com 293
http://georgiana.ca 70
http://feeds.feedburner.com 24

Accessibility

Upload Details

Uploaded via as Adobe PDF

Usage Rights

© All Rights Reserved

Report content

Flagged as inappropriate Flag as inappropriate
Flag as inappropriate

Select your reason for flagging this presentation as inappropriate.

Cancel
  • Full Name Full Name Comment goes here.
    Are you sure you want to
    Your message goes here
    Processing…
Post Comment
Edit your comment

    Intro to PHP for Beginners Intro to PHP for Beginners Presentation Transcript

    • FooLabPHP Workshop #1First Programming Experience
    • 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
    • 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
    • Programming FooLabDescribing procedures Input Find Password username username matches?and password Yes No Open Display error account page message 4
    • Functions FooLabOne step in the whole program pi 3.141592 ... 26, 58 max 58 Y-m-d date 2012-04-26 5
    • 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
    • Functions FooLab php> echo pi(); php> echo max(26,58); php> echo max (26, 58); php> echo date("Y-m-d"); 7
    • Interpreter FooLab• I like like php> echo echo;• I like the word “like” php> echo "echo"; 8
    • 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
    • Variables FooLab• "Programming is cool"• "Design is cool"• "Video editing is cool" php> $hobby = "Design"; php> echo $hobby; php> echo "$hobby is cool"; 10
    • 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
    • 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
    • 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
    • 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
    • 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
    • Movie Price FooLab• Movie costs 12$• Popcorn costs 8$• Popcorn can be shared between two people. 16
    • FooLab• What happens with popcorn when we have an odd number of people? ceil(3 / 2); 17
    • Procedure FooLabCalculate 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
    • 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
    • Answer FooLab<?php$people = 3;echo ceil($people / 2) * 8 + $people * 12;?> 20
    • FooLabFunctionsUnderstanding Them and Writing Your Own
    • FooLab 26, 58 max 58• Accept parameters• Perform steps• Return result 22
    • Create and Call a Function FooLab• Wrap code in a function: function movie_price($people) { $total = ceil($people / 2) * 8 + $people * 12; return $total; }• Call function: echo movie_price(5); 23
    • What Happened? FooLab Number of Price was Price came out people went in displayed (5) 84 echo movie_price 24
    • FooLabWorking With Text andNumbersUsing String and Math Functions
    • 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
    • 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
    • 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
    • Answer FooLab function area($h, $w) { return $h * $w; } function rand_letter() { $letters = "abcdefghijklmnopqrstuvwxyz"; return substr($letters, rand(0,25), 1); } 29
    • FooLabConditionsIf It’s Cold, Wear a Coat
    • FooLab• Expression: produces a value of true or false.• Execute a process only if the expression is true. 31
    • Writing ConditionalStatements FooLab• Open the script.php file in your text editor. $temp = 12; if ($temp < 15) { echo "Wear a coat"; } 32
    • FooLab$temp = 20;if ($temp < 15) { echo "Wear a coat";}else { echo "Its hot enough";} 33
    • Mental Exercise FooLab 1 < 1 1 == 1 1 != 1 34
    • Combining Expressions FooLab• Temperature is less than 5 degrees and wind is greater than 30 km/h 35
    • Logical Operators FooLab true && true true && false true || false false || false 36
    • FooLabRepetitionLather. Rinse. Repeat.
    • Vocabulary FooLab• Repetition (to repeat)• Loop (to loop)• Iteration (to iterate) 38
    • 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
    • What Happened? FooLab$i = 1; $i <= 3; $i++ $i <= 3 $i <= 3 $i <= 3 $i <= 3 echo $i echo $i echo $i stop $i = 2 $i = 3 $i = 4 40
    • Chorus FooLab for ($i = 1; $i <= 4; $i++) { echo "This song is just six words longn"; } 41
    • Array FooLabAn ordered set of related elements 42
    • Array FooLabAn ordered set of related elements 43
    • 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
    • Acces Elements By Index FooLab$movies = array("Titanic", "Shrek", "Die Hard");echo $movies[1]; 45
    • Iterating Over Arrays FooLab$movies = array("Titanic", "Shrek", "Die Hard");foreach ($movies as $movie) { echo "I watched $movien";} 46
    • 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
    • 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
    • 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
    • Answer FooLabfunction rand_vowel() { $vowels = "aeiou"; return substr($vowels, rand(0,4), 1);}function rand_consonant() { $consonants = "bcdfghjklmnpqrstvwxyz"; return substr($consonants, rand(0,20), 1);}function rand_password() { $pass = ""; for ($i = 1; $i <= 3; $i++ ) { $pass = $pass.rand_consonant().rand_vowel().rand_consonant(); } return $pass;} 50
    • 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
    • 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
    • Resources FooLab• php.net has a manual and a reference for all functions.• phpquebec.org is the PHP users group in Montreal. 53