An SEO’s Intro to Web Dev PHP

735 views
664 views

Published on

What is PHP?

Variables and Arrays

Functions

Loops and Conditions

Published in: Technology, Design
0 Comments
1 Like
Statistics
Notes
  • Be the first to comment

No Downloads
Views
Total views
735
On SlideShare
0
From Embeds
0
Number of Embeds
7
Actions
Shares
0
Downloads
3
Comments
0
Likes
1
Embeds 0
No embeds

No notes for slide

An SEO’s Intro to Web Dev PHP

  1. 1. An SEO’s Intro to Web Dev 2 Just PHP Troy Boileau | SEO & Inbound Marketing Consultant For Powered by Search | January 2014
  2. 2. We’re in business because we believe that great brands need both voice and visibility in order to connect people with what matters. A boutique, full-service digital marketing agency in Toronto, Powered by Search is a PROFIT HOT 50-ranked agency that delivers search engine optimization, pay per click advertising, local search, social media marketing, and online reputation management services. Some of our clients... Featured in...
  3. 3. What is PHP? Variables and Arrays Functions Loops and Conditions
  4. 4. What is PHP?
  5. 5. What is PHP? And Why Should I Care? PHP is a server-side programming language that’s used on 81% of all websites. But what does “Server-Side” mean? There’s two sides to this coin: server-side and client-side. Client-side code is run by a web browser and other technology on the user’s computer (like how Adobe Flash Player, a program on your computer, is used to run video files in your browser). Client-side code is usually HTML, CSS and JavaScript. The user downloads the code, the browser runs it. Server-side code is run by the web server. Since the server usually has access to a database and other hidden files and programs, it can do neat things like dynamically pull in information for a page. The server runs its code, then the users downloads it, then the browser runs client-side code.
  6. 6. What is PHP? And Why Should I Care? When you’re poking around in a client’s website trying to fix some SEO issue or another you’re going to run into this:
  7. 7. What is PHP? And Why Should I Care? When you’re poking around in a client’s website trying to fix some SEO issue or another you’re going to run into this:
  8. 8. What is PHP? And Why Should I Care? When you’re poking around in a client’s website trying to fix some SEO issue or another you’re going to run into this:
  9. 9. What is PHP? And Why Should I Care? And unless you have a general understanding of what this means and how it works, you’re going to have to go to a developer whose time we bill in the three digits per hour as well as waste everyone’s time for simple issues. Also, if you get a basic grasp of PHP you’ll understand every programming language in the world. Oh, and that little <?php is just the opening tag to any PHP segment. All PHP code segments are wrapped in this: <?php ?>
  10. 10. What is PHP? One Final Note You’ll forget all of this. Just try to understand it as we zip through. Go do the Code Academy courses. If after that you’re still not excited about programming, that’s fine. At least you’ll recognize little bits of code and be able to look them up on Google and get the gist of everything rather than running to an overwhelmed developer. If you are excited, good for you. Programming is about creating. Go build something big and you’ll learn more than any book will teach you.
  11. 11. Variables and Arrays
  12. 12. Variables and Arrays Building Blocks of Programming HTML lets us show users information like this: <p>Hello World!</p> That’s fine, we can do that with PHP too: What’s with the echo bit, you ask? It’s a command that forces PHP to output what follows. If you don’t use “echo” then nothing will get sent to the user. This code sends the following HTML to the browser: <p>Hello World!</p>
  13. 13. Variables and Arrays Building Blocks of Programming That’s a pretty lame use of PHP though. Let’s start using variables which are containers for different types of data (numbers, words, arrays, anything). Then you can call that same information back anywhere in the code, just by using the variable name.
  14. 14. Variables and Arrays Building Blocks of Programming Variables are fundamental to programming. Strings (what we call any kind of text) need to be wrapped in quotation marks “” while you can declare a numerical variable just need the number:
  15. 15. Variables and Arrays Arrays Imagine a variable as a name associated to a box. This one holds the value of 12 but it could just as easily be “Pineapples.” An array is a name associated to many boxes. Each box in an array is opened with a key, which is pretty easy to figure out. The key is a number starting at 0 from the first array element to the second (1), the third (2), and so on. To output “11” the code would be echo $array[0];. “golum” would be echo $array[4];. What would the code be for “44”?
  16. 16. Variables and Arrays Arrays Here’s a quick look at creating an array and what its guts look like. The code is: And here’s what shows up in the browser’s source:
  17. 17. Variables and Arrays Arrays Why arrays though? Well, it’ll make a lot more sense later when we get into loops, but consider how valuable it would be to have a list of 600 names stored in variables with different names. Probably not very. You might as well have an array called $names with 600 elements in it.
  18. 18. Questions?
  19. 19. Functions and DRY
  20. 20. Functions and DRY Don’t Repeat Yourself One of the most common, most basic programming rules is “Don’t Repeat Yourself.” For example, if I know I’m going to repeat the URL of a site a million times over, I’m going to just store the URL in a variable. There are lots of great reasons to respect DRY code. 1. Since I’m using a variable rather than manually putting the URL in each time, I don’t have the human error issue. 2. If we drop the www. bit of the URL, I just change one line of code rather than potentially hundreds. 3. I don’t have to write out (or remember) the whole URL over and over again.
  21. 21. Functions and DRY Fun with Functions... Functions are another useful feature for keeping your code neat. They have three parts: 1. A Name 2. Input Variables (optional) 3. A Return Value (optional) They all have the same format, too:
  22. 22. Functions and DRY Fun with Functions... Here’s a pretty useless function to demonstrate how they work: The first bit is the function declaration. It’s where I declare what I want the function to do. The second bit, where I call “what_i_do()” is a function call. You can only declare a function once, but you can call it a million times.
  23. 23. Functions and DRY Fun with Functions... Let’s make it a bit more interesting. Notice how I’m wrapping the input variable in HTML paragraph tags. Let’s not do that. I’m pretty lazy so I want the function to do that for me. Now we’re using all three parts of the function. The name, the input variable and the return value. And the function does something useful and time-saving; it wraps a string in a paragraph tag.
  24. 24. Questions?
  25. 25. Loops and Conditions
  26. 26. Loops and Conditions If This Then That Now we’ve got variables and functions, the next step is conditionals. We make conditional decisions all of the time; if there’s no milk, then I’ll go buy milk. In PHP, we can do the same thing. It just looks different. The bit inside the round brackets is a boolean expression. That sounds complex but all it means is that whatever you put in those brackets has to be either true or false. The complicated bit arises when we start talking about boolean operators.
  27. 27. Loops and Conditions Boolean Operators Let’s go over the most useful three operators really fast: 1. == (equal to) 2. < (less than) 3. > (greater than) Notice how the first on the list isn’t a single equal sign? That’s because PHP considers a single equal sign to mean “MAKE the left side equal the right side,” e.g. “$var = 1” MAKES the variable $var hold the value of 1. If you want a boolean (true/false) response then you’re always looking for the double equal sign.
  28. 28. Loops and Conditions Boolean Operators Now for some examples. Just say “true” or “false.” 1>2 2 == 1 1+7>9 2/1>1 $var = 7 $var == 9
  29. 29. Loops and Conditions Boolean McOperators Now for some examples. Just say “true” or “false.” If (1 > 2) { } If (2 == 1) { } If (1 + 7 > 9) { } If (2 / 1 > 1) { } $var = 7 If ($var == 9) { }
  30. 30. Loops and Conditions Marrying Conditions and Functions So, if this boolean operator is true, then do whatever. This code creates a function called test that accepts an input variable called name. It checks to see if the name is Spartacus, and if so outputs “I AM SPARTACUS”. Of the four names we put in, there’s only one Spartacus, so the output will be “I AM SPARTACUS” just the once.
  31. 31. Loops and Conditions Conditions and Functions But I think everyone was Spartacus in the movie, so let’s just subtly change it using an else statement, which appends onto an if statement but the code within it only runs if the conditional in the if statement was false. Let’s trace this code once. We send in “Timothy.” The code goes, “Is ‘Timothy’ that same as ‘Spartacus’? No? Then let’s use the code in the else statement and output ‘I’m Spartacus’.”
  32. 32. Questions?
  33. 33. Loops and Conditions Loops Loops are the last bit. Certain types of loops can be needlessly complex, but what they do is loop over a piece of code over and over again until the condition to break the loop is met. Here’s an example of a type of loop called a “while loop”: You should recognize the $number < 3 bit. It’s the same idea as in the if statement; it returns either true or false. The while statement checks the boolean statement, and if true it runs the code within the curly brackets, over and over until the statement is false.
  34. 34. Questions? (Yeah that was it)
  35. 35. Some Examples Understanding Your First PHP Program
  36. 36. Understanding PHP Everything Let’s do something simple but... maybe useful isn’t the right word... with loops, arrays, functions, conditionals, all of this. Remember arrays? We can use a loop to go through them. First, let me teach you a new array trick and let you take a look at the array we’re going to work with. Assigning a value to the array name followed by [] just adds it to the end of the array.
  37. 37. Understanding PHP Everything So we’ve got this array of names, and we want to know if they’re Spartacus or not. Let’s go back to our old function that solved this problem, but change it slightly: What would the output be if I wrote the line of code: who_is_spartacus(12); ?
  38. 38. Understanding PHP Everything Now we’ve got an array of names and a function that checks one name. This doesn’t work very well. We need to send just ONE name to the function from our array... This is where our loop becomes useful. And I get to teach you something else! A function called count(), which ... Counts the elements of an array. If an array has 4 elements, then, count(array_name) will return the number 4. Which you’d expect. So what happens in this loop?
  39. 39. Understanding PHP Everything Now what happens, keeping in mind that $names[0] is equal to “Sarah”?
  40. 40. Understanding PHP Everything And what would happen if we, god forbid, did this?
  41. 41. http://cutestpaw.com
  42. 42. Questions?
  43. 43. Get Out .... <3 Stay in Touch Twitter: @troyfawkes Google+: google.com/+TroyBoileau Email: troy@poweredbysearch.com www.poweredbysearch.com www.troyfawkes.com

×