back to contents

Web Dev: PHP to power it

what we're really talking about

First of all, where are we right now in this process?

If we're building a car, you should already know how to make the chassis. HTML/CSS is the chassis of the car. It's what people look at.

PHP will be the engine that makes the car go. I mean, you can definitely have a nice car with wheels and it looks great, but it kinda sucks when you have to move it yourself with your own feet Flinstones-style.

(Honestly though, you can make a great website using just HTML/CSS and nothing more complex than that. These guides are written entirely by hand in HTML, I'm not doing this in a database with PHP or anything.)

However, PHP is just one possible thing you could use for an engine. You could use Ruby on Rails if you wanted. Or Python. Or Perl. Or Java. Well, don't use Java.

But I'm going to teach you PHP because it's pretty ubiquitous among the major players on the web, and honestly I think it's a good way to start. (Because it's how I started.)

Really, I'm going to kind of shape this guide with broad concepts that apply to many programming languages, with PHP as the example language.

This is a loonnnggg guide. Because there's a lot to learn. Let's take a journey together...

what is PHP, exactly?

PHP stands for PHP Hypertext Preprocessor. A recursive title. Hilarious. Nerds.

Anyway, it's a language, kind of like how HTML is a langauge, but different. (Of course it's different; every language is different.) HTML is a markup language whereas PHP is a processing language. Both need to be interpreted and read by a computer before they do anything.

What's important is that PHP lives on the server side of things, whereas HTML lives on the client side. What that means is that when you write HTML and put it on the web, anyone can read it. That's why you can right-click on this page and view the Source code.

But for languages that live on the server-side, you never see that code. It goes like this:

  1. You type in
  2. The server says "OK, they want something.php, I'll do that script"
  3. The server reads the instructions in something.php and does it.
  4. In the browser, you see the result of that. The server does the script and hands the result to your browser.

If you were to View Source on that page, you wouldn't see any of the instructions in the php file, you'd just see the results of them. Just like how when you're viewing an HTML file in your browser, you don't see any of the tags, you just see the result of them!

But why use this? is a good question, mostly. For one thing, you may be asking the website for secure data, so it doesn't trust you to just go in there yourself. How would you like it if someone walked into your home unannounced and just started going through your things? PHP wants to go in there and get what you want without actually giving you access to what's behind the scenes.

The server may also be doing very complicated things that your browser just couldn't do. This is becoming less and less the case as time goes on, but sometimes it's still true. Generally it's safer to do it yourself on the server-side.

what for?

A server-side scripting language can be extremely useful. The #1 reason is typically attributed to the fact that you can have a magically dynamic website. What does that mean?

Well if you wanted to make a blog with just HTML and CSS, you'd need to make individual HTML files for every individual blog post! So if you have 30 blog posts, you'd probably have 30 HTML files.

What if you could make just one page that could display each of those blog posts depending on which one the user wanted? That's what they mean by dynamic. One page does many things depending on what the user wants or what data is available.

So using just HTML and CSS, for a blog, you'd need 30 blog post HTML files, an index file that has a list of the blogs, and you'd need to update all of them manually every time something changed.

With PHP, you could have just two files and a database: one file to display the index of all blog posts, one file to display individual posts, and a database of the actual posts.

That's what for.

what do we need to use PHP?

You need a server that supports PHP. Support for it isn't typically built into Windows or Mac. You can't really right-click on a PHP file and have it open in a browser and see it the same way you can with an HTML file.

So you have a few options:

So you have a way to host and view PHP files? Great. To actually write PHP scripts, you just need a plain text editor. Awesome, right? Just like HTML. So open NotePad or TextEdit again, same as before.

After you have that squared away, we can try this out with our first test file!


As is common with learning a new language, the first thing you learn is how to say "hello world". Well, it looks like this in PHP:


echo 'HELLO WORLD!';


That's it. Save it as haha.php, put it on your server, and then open it in a browser. All you should see are the words HELLO WORLD!

Just like in HTML, you can immediately see a similarity: you need to start the file by saying that it's going to be PHP with a PHP tag! It looks a little different than HTML though.

Every PHP file starts with <?php and ends with ?> (I'm not going to write them in every example; you need to remember to do so yourself.)

In between those tags, wherever they are, the server will understand that it needs to do whatever is between them before giving the file to your browser.

In this case, all we've written is that it needs to echo the string 'HELLO WORLD!'

This line ends with a semicolon, as almost every line should! Semicolons end statements. All of them. Remember that! There are some things you won't need to end with a semicolon, but I'll clearly highlight them.

We're deep in it now

It's all uphill from here. This is where you need to start learning concepts that may hurt your brain if you've never ever written any kind of programming language before. It's simply a matter of defining our universe by our own writing. We need to cover the following:

And there's probably more I'm forgetting. However, what's wonderful about this is that these are concepts that apply generally to the vast majority of programming languages. Once you learn one, you've laid the foundation to learn more! How exciting.

We'll cover them one at a time. Let's start with something simple.

Express yourself

One of the most basic concepts of all programming is that of expressions. It's so basic, you won't even think about it after you learn it.

Here's an expression:

4 + 10

Hahahaha... yeah it's that simple. An expression is any statement that will return a result!

4 + 10 returns 14. Let's break this down a little further... yes, it can be broken down further.

4 is an object. It's a number, to be specific. There are things you can do with a number, like add or multiply it to another number.

So in that above expression, we have the number 4, and we are performing an addition upon it, with 10 as the number to be added.

It will return the number 14, of course. But in the above expression, we're not capturing that result. It just happens, and then escapes into the ether between CPU cycles. But what if we want to do something with the result?

Life is a series of variables

We use variables to capture the results of expressions. Think of a variable as a reference or a symbol or just a holding area for something else. Here, let me show you:

$lol = 4 + 10;

The $lol part is a variable! In PHP, the dollar sign $ before a series of letters and numbers (no spaces) denotes a variable name. In this case, we've named the variable lol. Very apt.

So what does the variable do? It holds the result of 4 + 10. We use the equal sign = to assign the variable a value, or piece of data. So if you were to ask $lol what its value is, it would say 14.

What the most important thing you should learn right now is that, unlike math, the equal sign is used to assign values. Writing 4 + 10 = 14 in PHP doesn't mean anything. It'll actually throw an error, I think.

Notice that the $lol variable is on the left. The computer will read instructions from left to right, so it'll read the above expression as: with the variable $lol, assign it the result of 4 + 10. You couldn't write it any other way.

Remember: you're writing for a computer. It only knows how to do things in certain ways.

Data types define variables

With PHP, you don't need to worry about data types too much, since PHP kinda lets you do whatever. However, you will still need to know how they work.

Data types are simply how you define a piece of data. In the above example, we were using numbers. A number is a data type!

Specifically, 4 would be defined as an integer, or int. It's a solid number, positive or negative. 3.2 would be a floating-point number; one with a decimal point.

Other data types in PHP, and across most programming languages:

A Boolean is either true or false. That's it. (Trust me, it's an important data type.)

String. Think of it this way: "a" is a single character. "apple" is a string of individual characters. Hence, string. In PHP, you surround a string with single- or double-quotes to denote it as such, like so:

$lol = "hello!";

When you would ask $lol what it is, it doesn't include the quotation marks, but you needed to use them to denote it as a string. The quotation marks simply tell the computer, "Hey! This is a string!"

An Array is a collection of values, typically denoted by a comma-separated list inside the array() function, like so:

$fruits = array( "apple", "banana", "strawberry" );

In the above example, the variable $fruits contains three values: the strings apple, banana, and strawberry. You could put more in there if you wanted to.

Many would say that an array is a complex data type because it is a collection of other data types. This wouldn't be far off, but it's kind of irrelevant. The above array has three values, each is a string. You could have them be anything though, and however many you want.

$something = "la la la";
$lotsa_stuff = array( "a word", 24, 'delicious!', 2819.291, $something );

That array holds a lot of different types. Notice that you can use either single- or double-quotes to denote a string. Notice you can also put a variable in there! Crazy, right? Arrays just hold lots of stuff.

NULL is its own data type, too, even though it denotes nothingness. Haha, kinda existential, right? But it's very important. If a variable were to not yet have been set a value, it'd be considered NULL.


See? $var exists, we just told the computer it exists, but it hasn't been set to anything yet. So if we'd ask the computer what it was, it'd say NULL. NULL doesn't have any quotes around it, it's not a string. It's simply NULL.

One crazy thing about PHP is that it lets you re-assign a variable at will. This will work just fine:

$lol = 2000;
$lol = "2000";

Now we never told $lol that it was going to be a number, we just set it to 2000. Then we immediately changed it to be a string. (Even though there's a number within the quotation marks, the computer thinks it's a string because that's how it understands the quotation marks!)

Many languages wouldn't let you do that kind of variable data type changing, but PHP doesn't care. This is useful, but you have to keep it in mind. It means that sometimes you might have to first check and make sure a variable is a number before you perform number functions on it! You certainly can't perform a multiply on a string, that doesn't make sense.

a little more about strings

Strings are just a collection of characters. But there are a few important things to know about how we actually construct strings...

As I said, you can hold a string in single or double quotes, like so:

$a = 'hello';
$b = "hello";

What's the difference? In those two strings, there's no difference. However, double quotes allow you to do more. But first, what if we do this:

$a = 'I'm happy to see you.';
$b = "She said, "hello there!"";

Single quotes inside single quotes and double quotes inside double quotes cause PHP to panic and crash, haha. Technically, PHP will see $a as just I, since it's between single quotes. The rest of that line, it'll just be confused and crash.

Sooo how do you put a single quote inside a single-quoted string? Two options:

  1. Change it to be double-quotes! $a = "I'm happy to see you.";
  2. Escape the single-quote! $a = 'I\'m happy to see you.';

the first one is obvious, but the second one is much more important. What does escaping mean? Escaping uses this character \ to tell PHP that the next character is special.

Inside single quotes, the only thing you can escape is a single quote. So PHP reads that string, encounters the \ character, and knows that the next one (which is a single quote) is special. In this case, it ignores the single quote, and keeps reading the string.

If you were to print out the value of $a, it wouldn't have that slash in it. That's taken out automatically.

As I said before, double quotes are more powerful, but obey the same rules. You'd need to escape a double quote inside a double-quoted string, like so:

$b = "She said, \"hello there!\"";

That'll make PHP happy. But with double quotes, there are other special escape characters! Stuff like \n and \t

\n means new line, and \t means tab. Think about it - how would you write new line into a string? Or hitting the tab key? With PHP, you use these escape characters.

$c = "One line,\nAnd another.";

If you were to print that out, PHP interprets the \n as where a new line should begin! Similar to HTML's line break <br /> tag. This wouldn't work within single quotes.

One more thing: with double quotes, you can put variables inside quotes! Check this out:

$a = 'hello';
$b = "$a world!";

That's just magical. In the second string, PHP will insert whatever the value of $a is into the string!

Typically I stick with single quotes unless I really need the power of double quotes.

a little more about arrays

briefly, i should mention this. arrays come in two varieties: simple and associative. i've already shown you a simple array, it looks like this:

$an_array = array( 'hello', 'caio', 'hola' );

But how do I get a specific entry out of the array? Here's how:

echo $an_array[0];

That returns the first item. The square brackets immediately following the variable name accesses what's within the array. Arrays are zero-indexed, meaning the first element is 0, the second is 1, and so on. That'll take a little getting used to, I know.

So in the array, the 0 is the key that unlocks a certain value within the array, in this case the first value, which is hello.

What if we don't want to use numbers? What if we want to name the keys ourselves? Well, we can use an associative array:

$an_array = array( 'lol' => 'laugh out loud', 'lmao' => 'laughing my ass off' );

In this new array, there are only two items, and they're pairs of a key name and a value. lol is the key that unlocks laugh out loud. So if we try to access it, like this:

echo $an_array['lol'];

It'll return laugh out loud! So we are associating the key lol with the value laugh out loud by using the => between the key name and the value within an array. Get it?

Simple or associative, both are useful.

To add to the end of a simple array, you'd do so like this:

$an_array[] = 'bonjour';

Without putting a key between the brackets [ ], it'll assume you mean to assign the value to the next slot. So if the array has three items in it already, the above code will put the new value in the fourth slot.

With an associative array, you need to specify the key the new value will fit in, like so:

$an_array['rofl'] = 'roll on floor laughing';

We specified we want to add a value to the array $an_array under the key name 'rofl' with the value 'roll on floor laughing'.

Secret Agent Operators

Check this out and think about it... if the equal sign doesn't mean equals, like it does in math, what does? Again, think in terms of expressions:

100 == 100

That's an expression, but what does it result? We know that 4 + 10 would result in 14, but what does 100 == 100 return?

Quite simply, it returns true! It's totally true that 100 equals 100. However, you can't capture that in a variable the same way a + or - would. Instead, we use the == operator as a comparison operator.

You've probably seen other comparison operators before: < and > are also comparisons. They mean less than and greater than, respectively. Is 5 < 4? False. 5 > 4? True.

While == means equals, it's just as important to know that != means not equals! So what does this return:

5 != 4

It returns true! Because it's totally true that 5 does not equal 4. Likewise,

5 == 4

would return false, because 5 definitely does not equal 4. Get it?

So just to recap... in PHP, one equals sign acts to assign things. Double equal signs act to compare the equality of two things.

How would we use these, though?

Logic controls everything!

We use comparison operators with logic structures. That sounds intimidating... but it's not. Whenever you encounter something that sounds intimidating while you're learning programming, remember that it's probably not. You just need to break it down to understand it.

Here's a logic structure:

if (10 == 10) {
  echo "hello world!";

Oh boy! Our first flow control structure. What does it do? Well, read it! Try to read it like a sentence, ignoring the punctuation. it's pretty simple. If 10 equals 10, print out the string hello world! That's all.

But what exactly do we have here?

First of all, we have the keyword if, then we have an expression within parentheses. Next, we have curly braces which open { and close }. This basically means, to the computer, if whatever is within the ( ) is true, do whatever is in the { }

So if we wrote this:

if (5 > 10) {
  echo "we live in a crazy world.";

We wouldn't see any text, because 5 is definitely not bigger than 10. We could also make the expression more complicated:

if (5 + 4 > 10) {
  echo "a crazier world, still";

This works because the if statement will try to evaluate whatever is between the parentheses and see if it's true. In this case, it still isn't true! Haha. What if we want it to do something in case it isn't true? It's pretty simple:

if (5 + 4 > 10) {
  echo "a crazier world, still";
} else {
  echo "the world seems sane.";

We attach the else right after the first set of instructions and add more stuff between new curly braces, which signify what the computer should do in case the if statement is false.

So if we ran that code, we would see the world seems sane. because the expression within the if statement is false, hence the computer knows to do whatever is within the else statement.

Let's take a break...


So far we've gone through many constructs of a programming language. We've been talking about syntax (like the use of parentheses and curly braces) and keywords (like if and else) and so forth.

Next we need to learn about functions and what they do and how we can use them. I'll throw this out at you:

function lol() {
  echo 4 + 10;

That's a lot to digest, but try to stay calm. Don't be panicked by how it looks, we'll work through it together. Let's pick it apart.

First of all, understand that a function merely does something, and can possibly return a value after doing whatever it does, the same way an expression returns a value. A function merely incapsulates instructions into one place, usually so you don't need to re-write things you do a lot.

At the beginning of that snippet, we have the keyword function. That tells the computer to expect a function name, the same way the keyword if tells the computer to expect an expression to see if it's true.

Next we have the name of the function, in this case lol. You can call it whatever you like as long as there are no spaces in the name.

The parentheses next to the name allows arguments to be passed to the function. Don't worry about that yet, we'll get to it. But you need the parentheses there regardless, even if they're empty.

After that, once again, we see some curly braces. Between these are what we want the function to do!

Within the above function, it prints out the result of 4 + 10 by itself if we were to call upon it to do so. That's one way of doing things. Here's another:

function lol() {
  return 4 + 10;

echo lol();

Within this new function, we have the keyword return and then the same expression. We know that it would return 14.

The return keyword means give whatever called the function the result of the following expression. In this case, 14.

So what called the function? The echo did! And it'll get 14, and print it out for us to see. Let's go deeper:

function add($a, $b) {
  $c = $a + $b;
  return $c;

echo add(3, 5);
echo add(8, 111);

Oh dear! What does this mean? Again, don't panic, let's work through this.

We've made a function called add and it takes two arguments. (The arguments are comma-separated.) Within the function, these arguments are represented by the variables $a and $b. What we do with those variables is up to us, we need to write the instructions within the function.

In the case of the add function, we want it to add the two variables together and assign the result to the new variable $c.

Next, we want it to spit out the value of $c to whatever called the function.

In the next lines, we see the actual execution of the function a couple times! First, we see it pass the arguments 3 and 5. So what will our function do? Add them and spit out the result!

So the first one will print out the number 8 and the second one will print out the number 119. Likewise, you could do this:

function add($a, $b) {
  $c = $a + $b;
  return $c;

$omg = add(3, 5);

if ($omg > 7) {
  echo add($omg, 111);

Now we're just getting crazy, huh? Read it like it they're just sentences. Try reading it how a computer would.

  1. We have a function that adds two variables. The function returns the result.
  2. We have a variable that gets assigned the result of adding 3 and 5 with the function.
  3. If that variable is greater than 7, it does whatever is within the if statement.
  4. Within the if clause, it echoes out the result of another call to the add function.
  5. That add function call even uses the $omg variable! And why not?

If you can understand all that, then you've conquered the hard part. Seriously, you have. If you can think through that like a computer would, then you're good so far.

If you don't understand it yet, go back and read the past few sections until you do. Try to remember to break things down into pieces as I've described. Try to think the way a computer would.

PHP will tell you when it's broken

If you haven't already caused an error, then congrats. But I'm sure you might've already. PHP will definitely tell you when there's an error. Instead of running, it'll error out, and you'll see a warning page.

Nine times outta ten, it's probably because you forgot to put a semicolon somewhere. Or you forgot to close a parentheses or a curly brace. So make sure you go back through your code and make sure that's not the case.

Otherwise, google it!

Loops like rollercoasters, but not really

Another important concept in programming is loops, or iterators. They do something until told not to anymore, or whenever they run out of things to do! They've important.

Here's a loop example, it does something very simple:

for ($what = 1; $what < 10; $what = $what + 1) {
  echo $what;

Try it! Put it in a php document and run it. It'll simply print out all the numbers between 1 and 10 (not including 10). Again, let's break this down.

First of all, we have the for keyword, which tells the computer to expect some instructions on what to do. Like the if statement, it requires instructions inside parentheses.

However, unlike if, the for keyword uses three expressions instead of one. They are separated by semicolons.

The first one is this: $what = 1 and it signifies where the loop should start. It starts with $what equalling 1.

The next expression is this: $what < 10 and it signifies where the loop should stop. When that expression is false, stop! So as long as $what is less than 10, keep going.

The final one is: $what = $what + 1 and it tells the computer what to do after every statement.

So it goes like this...

  1. start off with the variable $what equalling 1
  2. keep going until ($what < 10) is false
  3. every time the computer does whatever is inside the curly braces, add 1 to $what
  4. for each iteration in the loop, echo whatever $what currently is

Instead of writing out $what = $what + 1 you could simply write $what++

The ++ is an increment; it simply adds 1 to the variable. It's a shortcut, which is why most loops will look like this:

for ($what = 1; $what < 10; $what++) {
  echo $what;

You could write it another way, too! So many ways to do the same thing.

$what = 1;

while ($what < 10) {
  echo $what;

That does the same thing as before, but more explicitly. We start off by setting the $what variable to 1.

Next, we use the keyword while, and we say while $what is less than 10, do the following.

Inside the curly braces we put our statement. It's important that you have to add a way for $what to no longer be less than 10, or else the while loop will go on forever!

That's why we include the $what++ inside the while loop as the last line. Every time it goes through the loop, make sure you increment the $what by 1.

The most important lesson to learn from this is that there are a lot of ways to do loops, each with their own benefits and drawbacks.

that's kinda it

I mean, those are the fundamentals. Those are the basics. Where do you go from here? Well, you apply the basics to problems. Next, I'll explain some more important stuff that they don't really teach you elsewhere.

Most guides teach you these basics of syntax and structure and then forget to teach you what to do with it or what else is available.

things built in to PHP

You notice that echo was used throughout this, but I've never really explained what it is. Only what it does.

echo just outputs the result of an expression. It's just a function built into PHP. You could write echo two ways:

echo "hello";

For echo, either is the same. You see in the second example, however, how much it's like our previous add function. echo takes one argument and then prints out the result for us to see.

Like echo, there are tons of functions built in to PHP. Like, really, tons.

Just look at this list of function libraries on the left. The functions built in to PHP come in libraries. Some of the come with almost every install of PHP, some of them you need to add manually.

What's built into PHP? Libraries that let you access databases that store information, or other libraries that get contents of other files to either use as data or run more PHP.

There's whole boatloads of functions that do things to the basic data types! For example, the trim function removes white-space from the beginning or end of a string, like so:

echo trim("   hello!        ");

That would print out hello! without all those spaces around it. The count function returns the amount of items in an array, like so:

$stuff = array( 'flower', 'apple', 'razor' );
echo count($stuff);

That would print out 3, because that's how many items are within the $stuff array.

I mean, just look at all these String functions! There's a lot you can do with what is already available to you by default in PHP.

a few more comments

Honestly I don't know why I didn't tell you this before, but then again I don't really know where I would've fitted it in.

First of all, comments let you insert text into your code without actually having it do anything. For example:

$what = 20;
// ok but this line doesn't do anything

That second line really doesn't do anything because it has the double slashes before it. This is called a commented-out line. The computer will ignore it.

You could also write it this way:

$what = 20;
anything between that weird combo of characters and the next pair
will totally be ignored

i can do whatever between them and the computer will ignore it!

So everything between /* and */ is ignored. That's for multi-line comments, in case you don't want to write // before every line.

Commenting-out lines is especially useful when you're trying to figure out what's broken in your code... you can selectively disable parts of it without having to delete anything. Get it?

$what = 20;
// $what = "hello!";

The computer will ignore the second line, hence keeping the value of $what at 20.

Mixing PHP and HTML

PHP is great because it can be easily mixed with HTML... check this out, it's a complete file:



for ($i = 0; $i < 10; $i++) {
  echo '<p>';
  echo $i;
  echo '</p>';



That's PHP code within an HTML page! If you save this as whatevs.php, put it on your server, and then go to it, you'll see the numbers 0 through 9.

If you look at the source of the page, all you'll see is 10 sets of p tags with the numbers 0 through 9 inside them.

This clearly illustrates how the server takes the PHP file and then does whatever is within the php tags and then shows you the result in your browser.

That's what I mean when I say PHP is the engine and HTML is the chassis.


More things to know about! Oh there's just too much knowledge, too little time...

There are variables built in to PHP the same way a ton of functions are. They are called superglobals, in that they work anywhere, really.

Here's one of the most useful ones, at least until you figure out the rest of them:

echo $_GET['what'];

Now save that as gettt.php, upload it, and load it up in a browser. Yup, you won't see anything. But try this: in your browser, add ?what=hello to the end of it!

The filename should now look like this: gettt.php?what=hello If you hit enter, you'll now see hello on the page. AMAZING. What did we just do!?

The question mark in the URL denotes a query string, specifically of the GET variety. It means, as a question mark often does, that you're asking for something.

Inside a query string are name-value pairs. The name of the GET variable here is what and its value is hello.

The $_GET variable allows you to access that query string, exactly the same as an associative array. The keys of the array will be the names in the name-value pairs.

You can add more by using the ampersand & character, like so:


So in that situation, there would be two keys inside the $_GET array: one is what, one is lol.

Similarly, when you start using forms in pages, you'll probably access the results of them through the $_POST variable.

And if you want to save a variable on the user's browser, like Google does, you can use cookies with the $_COOKIE variable!

I know this might all sound crazy... but it'll get easier with time. You just need to start doing things.

continuing on...

Oh lawd, this has been one long guide... I think I need a separate guide just to show you examples!

I will show you a big huge working example after you read the rest of the web dev guides. But here are some exercises you can do on your own if you wish: