SlideShare ist ein Scribd-Unternehmen logo
1 von 43
Downloaden Sie, um offline zu lesen
An SEO’s Intro to Web Dev 2
Just PHP

Troy Boileau | SEO & Inbound Marketing Consultant

For Powered by Search | January 2014
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...
What is PHP?
Variables and Arrays
Functions
Loops and Conditions
What is 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.
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:
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:
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:
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
?>
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.
Variables and Arrays
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>
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.
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:
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”?
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:
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.
Questions?
Functions and DRY
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.
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:
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.
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.
Questions?
Loops and Conditions
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.
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.
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
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) {
}
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.
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’.”
Questions?
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.
Questions? (Yeah that was it)
Some Examples

Understanding Your First PHP
Program
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.
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);
?
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?
Understanding PHP
Everything
Now what happens, keeping in mind that $names[0] is equal to “Sarah”?
Understanding PHP
Everything
And what would happen if we, god forbid, did this?
http://cutestpaw.com
Questions?
Get Out
....
<3

Stay in Touch
Twitter: @troyfawkes
Google+: google.com/+TroyBoileau
Email: troy@poweredbysearch.com
www.poweredbysearch.com
www.troyfawkes.com

Weitere ähnliche Inhalte

Was ist angesagt?

Class 1 handout (2) html exercises
Class 1 handout (2) html exercisesClass 1 handout (2) html exercises
Class 1 handout (2) html exercises
Erin M. Kidwell
 
Creating Web Sites with HTML and CSS
Creating Web Sites with HTML and CSSCreating Web Sites with HTML and CSS
Creating Web Sites with HTML and CSS
BG Java EE Course
 

Was ist angesagt? (20)

HTML Basics by software development company india
HTML Basics by software development company indiaHTML Basics by software development company india
HTML Basics by software development company india
 
Class 1 handout (2) html exercises
Class 1 handout (2) html exercisesClass 1 handout (2) html exercises
Class 1 handout (2) html exercises
 
Basic HTML
Basic HTMLBasic HTML
Basic HTML
 
Introduction to html fundamental concepts
Introduction to html fundamental conceptsIntroduction to html fundamental concepts
Introduction to html fundamental concepts
 
The Difference between HTML, XHTML & HTML5 for Beginners
The Difference between HTML, XHTML & HTML5 for BeginnersThe Difference between HTML, XHTML & HTML5 for Beginners
The Difference between HTML, XHTML & HTML5 for Beginners
 
Html for beginners part I
Html for beginners part IHtml for beginners part I
Html for beginners part I
 
HTML
HTMLHTML
HTML
 
html
htmlhtml
html
 
php 1
php 1php 1
php 1
 
Code This, Not That: 10 Do's and Don'ts For Learning HTML
Code This, Not That: 10 Do's and Don'ts For Learning HTMLCode This, Not That: 10 Do's and Don'ts For Learning HTML
Code This, Not That: 10 Do's and Don'ts For Learning HTML
 
HTML Basic, CSS Basic, JavaScript basic.
HTML Basic, CSS Basic, JavaScript basic.HTML Basic, CSS Basic, JavaScript basic.
HTML Basic, CSS Basic, JavaScript basic.
 
Html basics
Html basicsHtml basics
Html basics
 
Lecture 2 introduction to html basics
Lecture 2 introduction to html basicsLecture 2 introduction to html basics
Lecture 2 introduction to html basics
 
Creating Web Sites with HTML and CSS
Creating Web Sites with HTML and CSSCreating Web Sites with HTML and CSS
Creating Web Sites with HTML and CSS
 
Html and Xhtml
Html and XhtmlHtml and Xhtml
Html and Xhtml
 
Html
HtmlHtml
Html
 
Hyper text markup Language
Hyper text markup LanguageHyper text markup Language
Hyper text markup Language
 
Session4
Session4Session4
Session4
 
Basic html
Basic htmlBasic html
Basic html
 
HTML (Hyper Text Markup Language)
HTML (Hyper Text Markup Language)HTML (Hyper Text Markup Language)
HTML (Hyper Text Markup Language)
 

Ähnlich wie An SEO’s Intro to Web Dev PHP

1336333055 php tutorial_from_beginner_to_master
1336333055 php tutorial_from_beginner_to_master1336333055 php tutorial_from_beginner_to_master
1336333055 php tutorial_from_beginner_to_master
jeeva indra
 
&lt;img src="../i/r_14.png" />
&lt;img src="../i/r_14.png" />&lt;img src="../i/r_14.png" />
&lt;img src="../i/r_14.png" />
tutorialsruby
 
&lt;b>PHP&lt;/b> Reference: Beginner to Intermediate &lt;b>PHP5&lt;/b>
&lt;b>PHP&lt;/b> Reference: Beginner to Intermediate &lt;b>PHP5&lt;/b>&lt;b>PHP&lt;/b> Reference: Beginner to Intermediate &lt;b>PHP5&lt;/b>
&lt;b>PHP&lt;/b> Reference: Beginner to Intermediate &lt;b>PHP5&lt;/b>
tutorialsruby
 
PHP Basics Ebook
PHP Basics EbookPHP Basics Ebook
PHP Basics Ebook
Swanand Pol
 
Php tutorial(w3schools)
Php tutorial(w3schools)Php tutorial(w3schools)
Php tutorial(w3schools)
Arjun Shanka
 

Ähnlich wie An SEO’s Intro to Web Dev PHP (20)

php
phpphp
php
 
1336333055 php tutorial_from_beginner_to_master
1336333055 php tutorial_from_beginner_to_master1336333055 php tutorial_from_beginner_to_master
1336333055 php tutorial_from_beginner_to_master
 
PHP - Introduction to PHP - Mazenet Solution
PHP - Introduction to PHP - Mazenet SolutionPHP - Introduction to PHP - Mazenet Solution
PHP - Introduction to PHP - Mazenet Solution
 
&lt;img src="../i/r_14.png" />
&lt;img src="../i/r_14.png" />&lt;img src="../i/r_14.png" />
&lt;img src="../i/r_14.png" />
 
&lt;b>PHP&lt;/b> Reference: Beginner to Intermediate &lt;b>PHP5&lt;/b>
&lt;b>PHP&lt;/b> Reference: Beginner to Intermediate &lt;b>PHP5&lt;/b>&lt;b>PHP&lt;/b> Reference: Beginner to Intermediate &lt;b>PHP5&lt;/b>
&lt;b>PHP&lt;/b> Reference: Beginner to Intermediate &lt;b>PHP5&lt;/b>
 
PHP Basics Ebook
PHP Basics EbookPHP Basics Ebook
PHP Basics Ebook
 
phptutorial
phptutorialphptutorial
phptutorial
 
phptutorial
phptutorialphptutorial
phptutorial
 
Php tutorial(w3schools)
Php tutorial(w3schools)Php tutorial(w3schools)
Php tutorial(w3schools)
 
Php tutorialw3schools
Php tutorialw3schoolsPhp tutorialw3schools
Php tutorialw3schools
 
Bavpwjs1113
Bavpwjs1113Bavpwjs1113
Bavpwjs1113
 
php41.ppt
php41.pptphp41.ppt
php41.ppt
 
PHP InterLevel.ppt
PHP InterLevel.pptPHP InterLevel.ppt
PHP InterLevel.ppt
 
php-I-slides.ppt
php-I-slides.pptphp-I-slides.ppt
php-I-slides.ppt
 
Php i-slides
Php i-slidesPhp i-slides
Php i-slides
 
Php i-slides
Php i-slidesPhp i-slides
Php i-slides
 
Php i-slides
Php i-slidesPhp i-slides
Php i-slides
 
Php i-slides (2) (1)
Php i-slides (2) (1)Php i-slides (2) (1)
Php i-slides (2) (1)
 
Php web development
Php web developmentPhp web development
Php web development
 
Javascript breakdown-workbook
Javascript breakdown-workbookJavascript breakdown-workbook
Javascript breakdown-workbook
 

Kürzlich hochgeladen

Kürzlich hochgeladen (20)

Manulife - Insurer Innovation Award 2024
Manulife - Insurer Innovation Award 2024Manulife - Insurer Innovation Award 2024
Manulife - Insurer Innovation Award 2024
 
AWS Community Day CPH - Three problems of Terraform
AWS Community Day CPH - Three problems of TerraformAWS Community Day CPH - Three problems of Terraform
AWS Community Day CPH - Three problems of Terraform
 
Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...
Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...
Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...
 
Apidays New York 2024 - The value of a flexible API Management solution for O...
Apidays New York 2024 - The value of a flexible API Management solution for O...Apidays New York 2024 - The value of a flexible API Management solution for O...
Apidays New York 2024 - The value of a flexible API Management solution for O...
 
MINDCTI Revenue Release Quarter One 2024
MINDCTI Revenue Release Quarter One 2024MINDCTI Revenue Release Quarter One 2024
MINDCTI Revenue Release Quarter One 2024
 
Polkadot JAM Slides - Token2049 - By Dr. Gavin Wood
Polkadot JAM Slides - Token2049 - By Dr. Gavin WoodPolkadot JAM Slides - Token2049 - By Dr. Gavin Wood
Polkadot JAM Slides - Token2049 - By Dr. Gavin Wood
 
2024: Domino Containers - The Next Step. News from the Domino Container commu...
2024: Domino Containers - The Next Step. News from the Domino Container commu...2024: Domino Containers - The Next Step. News from the Domino Container commu...
2024: Domino Containers - The Next Step. News from the Domino Container commu...
 
Connector Corner: Accelerate revenue generation using UiPath API-centric busi...
Connector Corner: Accelerate revenue generation using UiPath API-centric busi...Connector Corner: Accelerate revenue generation using UiPath API-centric busi...
Connector Corner: Accelerate revenue generation using UiPath API-centric busi...
 
Artificial Intelligence Chap.5 : Uncertainty
Artificial Intelligence Chap.5 : UncertaintyArtificial Intelligence Chap.5 : Uncertainty
Artificial Intelligence Chap.5 : Uncertainty
 
Exploring the Future Potential of AI-Enabled Smartphone Processors
Exploring the Future Potential of AI-Enabled Smartphone ProcessorsExploring the Future Potential of AI-Enabled Smartphone Processors
Exploring the Future Potential of AI-Enabled Smartphone Processors
 
presentation ICT roal in 21st century education
presentation ICT roal in 21st century educationpresentation ICT roal in 21st century education
presentation ICT roal in 21st century education
 
Deploy with confidence: VMware Cloud Foundation 5.1 on next gen Dell PowerEdg...
Deploy with confidence: VMware Cloud Foundation 5.1 on next gen Dell PowerEdg...Deploy with confidence: VMware Cloud Foundation 5.1 on next gen Dell PowerEdg...
Deploy with confidence: VMware Cloud Foundation 5.1 on next gen Dell PowerEdg...
 
Data Cloud, More than a CDP by Matt Robison
Data Cloud, More than a CDP by Matt RobisonData Cloud, More than a CDP by Matt Robison
Data Cloud, More than a CDP by Matt Robison
 
ProductAnonymous-April2024-WinProductDiscovery-MelissaKlemke
ProductAnonymous-April2024-WinProductDiscovery-MelissaKlemkeProductAnonymous-April2024-WinProductDiscovery-MelissaKlemke
ProductAnonymous-April2024-WinProductDiscovery-MelissaKlemke
 
Real Time Object Detection Using Open CV
Real Time Object Detection Using Open CVReal Time Object Detection Using Open CV
Real Time Object Detection Using Open CV
 
Apidays New York 2024 - Scaling API-first by Ian Reasor and Radu Cotescu, Adobe
Apidays New York 2024 - Scaling API-first by Ian Reasor and Radu Cotescu, AdobeApidays New York 2024 - Scaling API-first by Ian Reasor and Radu Cotescu, Adobe
Apidays New York 2024 - Scaling API-first by Ian Reasor and Radu Cotescu, Adobe
 
Bajaj Allianz Life Insurance Company - Insurer Innovation Award 2024
Bajaj Allianz Life Insurance Company - Insurer Innovation Award 2024Bajaj Allianz Life Insurance Company - Insurer Innovation Award 2024
Bajaj Allianz Life Insurance Company - Insurer Innovation Award 2024
 
TrustArc Webinar - Unlock the Power of AI-Driven Data Discovery
TrustArc Webinar - Unlock the Power of AI-Driven Data DiscoveryTrustArc Webinar - Unlock the Power of AI-Driven Data Discovery
TrustArc Webinar - Unlock the Power of AI-Driven Data Discovery
 
Boost PC performance: How more available memory can improve productivity
Boost PC performance: How more available memory can improve productivityBoost PC performance: How more available memory can improve productivity
Boost PC performance: How more available memory can improve productivity
 
Axa Assurance Maroc - Insurer Innovation Award 2024
Axa Assurance Maroc - Insurer Innovation Award 2024Axa Assurance Maroc - Insurer Innovation Award 2024
Axa Assurance Maroc - Insurer Innovation Award 2024
 

An SEO’s Intro to Web Dev PHP

  • 1. An SEO’s Intro to Web Dev 2 Just PHP Troy Boileau | SEO & Inbound Marketing Consultant For Powered by Search | January 2014
  • 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. What is PHP? Variables and Arrays Functions Loops and Conditions
  • 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. 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. 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. 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. 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. 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.
  • 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. 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. 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. 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. 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. 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.
  • 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. 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. 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. 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.
  • 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. 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. 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. 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. 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. 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’.”
  • 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.
  • 35. Some Examples Understanding Your First PHP Program
  • 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. 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. 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. Understanding PHP Everything Now what happens, keeping in mind that $names[0] is equal to “Sarah”?
  • 40. Understanding PHP Everything And what would happen if we, god forbid, did this?
  • 43. Get Out .... <3 Stay in Touch Twitter: @troyfawkes Google+: google.com/+TroyBoileau Email: troy@poweredbysearch.com www.poweredbysearch.com www.troyfawkes.com