43
An SEO’s Intro to Web Dev 2 Just PHP Troy Boileau | SEO & Inbound Marketing Consultant For Powered by Search | January 2014

An SEO’s Intro to Web Dev PHP

Embed Size (px)

DESCRIPTION

What is PHP? Variables and Arrays Functions Loops and Conditions

Citation preview

Page 1: An SEO’s Intro to Web Dev PHP

An SEO’s Intro to Web Dev 2 Just PHP

Troy Boileau | SEO & Inbound Marketing Consultant

For Powered by Search | January 2014

Page 2: An SEO’s Intro to Web Dev PHP

Some of our clients...

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.

Featured in...

Page 3: An SEO’s Intro to Web Dev PHP

What is PHP?

Variables and Arrays

Functions

Loops and Conditions

Page 4: An SEO’s Intro to Web Dev PHP

What is PHP?

Page 5: An SEO’s Intro to Web Dev PHP

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.

Page 6: An SEO’s Intro to Web Dev PHP

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:

Page 7: An SEO’s Intro to Web Dev PHP

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:

Page 8: An SEO’s Intro to Web Dev PHP

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:

Page 9: An SEO’s Intro to Web Dev PHP

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 ?>

Page 10: An SEO’s Intro to Web Dev PHP

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.

Page 11: An SEO’s Intro to Web Dev PHP

Variables and Arrays

Page 12: An SEO’s Intro to Web Dev PHP

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>

Page 13: An SEO’s Intro to Web Dev PHP

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.

Page 14: An SEO’s Intro to Web Dev PHP

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:

Page 15: An SEO’s Intro to Web Dev PHP

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”?

Page 16: An SEO’s Intro to Web Dev PHP

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:

Page 17: An SEO’s Intro to Web Dev PHP

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.

Page 18: An SEO’s Intro to Web Dev PHP

Questions?

Page 19: An SEO’s Intro to Web Dev PHP

Functions and DRY

Page 20: An SEO’s Intro to Web Dev PHP

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.

Page 21: An SEO’s Intro to Web Dev PHP

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:

Page 22: An SEO’s Intro to Web Dev PHP

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.

Page 23: An SEO’s Intro to Web Dev PHP

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.

Page 24: An SEO’s Intro to Web Dev PHP

Questions?

Page 25: An SEO’s Intro to Web Dev PHP

Loops and Conditions

Page 26: An SEO’s Intro to Web Dev PHP

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.

Page 27: An SEO’s Intro to Web Dev PHP

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.

Page 28: An SEO’s Intro to Web Dev PHP

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

Page 29: An SEO’s Intro to Web Dev PHP

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) { }

Page 30: An SEO’s Intro to Web Dev PHP

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.

Page 31: An SEO’s Intro to Web Dev PHP

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’.”

Page 32: An SEO’s Intro to Web Dev PHP

Questions?

Page 33: An SEO’s Intro to Web Dev PHP

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.

Page 34: An SEO’s Intro to Web Dev PHP

Questions? (Yeah that was it)

Page 35: An SEO’s Intro to Web Dev PHP

Some Examples

Understanding Your First PHP Program

Page 36: An SEO’s Intro to Web Dev PHP

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.

Page 37: An SEO’s Intro to Web Dev PHP

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); ?

Page 38: An SEO’s Intro to Web Dev PHP

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?

Page 39: An SEO’s Intro to Web Dev PHP

Understanding PHP Everything

Now what happens, keeping in mind that $names[0] is equal to “Sarah”?

Page 40: An SEO’s Intro to Web Dev PHP

Understanding PHP Everything

And what would happen if we, god forbid, did this?

Page 41: An SEO’s Intro to Web Dev PHP

http://cutestpaw.com

Page 42: An SEO’s Intro to Web Dev PHP

Questions?

Page 43: An SEO’s Intro to Web Dev PHP

Get Out

....

<3

Stay in Touch

Twitter: @troyfawkes Google+: google.com/+TroyBoileau Email: [email protected]

www.poweredbysearch.com

www.troyfawkes.com