BUILDING A WEBSITE | PART 7 – BASIC PHP

POSTED ON NOVEMBER 4, 2011 BY MATTSICH |

Today we’re going to try to get the basic gist of PHP. This ain’t yo mama’s markup language. PHP is an actual programing language. We won’t cover everything because there’s just too much to learn all at once and we just don’t need it for wordpress themes. Let’s get started.

First of all we need to be able to view a php page. You can’t just open up a php page like an html page in a browser. You need to view it through a server. What we will do (instead of paying for a hosting service) we will use a local hosting application. Both mac and pc can use an app called “XAMPP” (DOWNLOAD HERE) so we will just use that. After you download it, install it and follow the instructions. You will end up with a htdocs folder where you will be able to place all your files that you want to view in your browser.

Let’s say you place the design project files that I supplied in the “Building the Website” tutorial in that folder (the file should be named ‘thedesign’). Now if you open your browser and go to http://localhost/thedesign/ , you should see the page.

We will be using ‘thedesign’ template as our sandbox (place for testing). Open up the folder and change the extension of ‘index.html’ to ‘index.php’. Refresh your browser now and notice that nothing has changed. This is because generally all that a php document does is allows you to use php in it. You still usually use html elements to put the php to use.

In order to write php, you need to have an opening and closing php tag.

1
2
3
<?php

?>

Now that we have those tags, we can write php inside them.

A little side note about php: If you have ever programed with some other language, you may remember that in order to use a variable, you needed to declare it… You don’t need to do this in php because php is a loosely typed language.

First of all, let’s get a simple ‘hello world’ on the screen. On line 46 of the index.php you will find a paragraph tag with a long string of text in it. Remove the text but leave the paragraph tags. Add the php tags inside the paragraph tags and write the following:

1
2
3
4
5
<p>
     <?php
          echo("Hello World!");
     ?>
</p>

Now if you refresh your browser, you should see ‘Hello World!’ appear. What we did here is use the echo function. When you pass a string of text or a variable inside it, it prints it out. Notice that I ended the statement with a semicolon! People forget these all the time and the code won’t work right without them. Notice also that I placed the text inside quotations. I did this to represent that it’s text and not a variable.

Now let’s declare a variable and pass it into the echo function to have it echo it out. Change the code to the following:

1
2
3
4
5
6
7
8
9
<p>
     <?php
         
          $theWord = "Hello World!"; //the variable declaration
         
          echo($theWord); /* echoing out the variable */

     ?>
</p>

Nothing really changed here except we set a variable to the text and then echoed out the variable (which in effect echoed out the text). Notice that a variable name is declared with the dolor sign in front of it.
The “=” does not mean “equals”. It means “assigned.” You don’t make something equal to a variable but you can assigned it.
Also notice that I don’t have the quotes around the variable when it’s inside the echo function.. it’s not a string of characters. Oh and you can write comments in the two ways I did in the code above ;)

Ok now let’s perform some simple operations. Write the following (you don’t need to write the comments. They are just there for you):

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
<?php
         
     $num1 = 1; //the variable declaration
     $num2 = 5; //the variable declaration
         
/* ADDITION */
     $result1 = $num1 + $num2; //add the two variables and set them to equal to a new one
     echo "The added result is:" . $result1 . "."; //echo out the result with some text.

/* SUBTRACTION */
     $result2 = $num1 - $num2; //subtract the two variables and set them to equal to a new one
     echo " The subtracted result is:" . $result2 . "."; //echo out the result with some text.

/* MULTIPLICATION */
     $result3 = $num1 * $num2; //multiply the two variables and set them to equal to a new one
     echo " The multiplication result is:" . $result3 . "."; //echo out the result with some text.

/* DIVISION */
     $result4 = $num1 / $num2; //divide the two variables and set them to equal to a new one
     echo " The devision result is:" . $result4 . "."; //echo out the result with some text.

/* MODULUS */
     $result5 = $num1 % $num2; //divide the two variables and set the REMAINDER equal to a new one
     echo " The remainder result is:" . $result5 . "."; //echo out the result with some text.

?>

Ok so simple stuff but I changed a few things. Notice that the parentheses are not necessarily required when using the echo function. Also look at how I echoed out full statements in one line. I use the period to connect the string of characters with the variables.
Plus, minus, times and divide are all familiar but if you have never done any programing, chances are that you have never heard of modulus. Let’s say you divide 6 by 3.. you get 2 with a remainder of 0. If you divide 6 by 5, you get 1 with a remainder of 1…. see what it is now? Modulus returns the remainder.
Now take a look at how this looks in your browser. If everything looks right, let’s move on.

Let’s say you want to divide a variable by another variable and set it equal to the first variable. Simple, right? Just write:

1
var1 = var1 / var2;

But there is a better and faster way to do this. You can write it this way instead:

1
var1 /= var2;

And then you can do the same with addition (+=), subtraction (-=), and multiplication (*=).

Next let’s write some conditional statements.

CONDITIONAL STATEMENTS

The first one we will look at is the if/else statement. Change your code to the following:

1
2
3
4
5
6
7
8
9
<?php  
     $num1 = 5; //declare the variable to be 5
 
     if($num1 == 5){
          echo "HEY! num1 is equal to 5!";
     }else{
          echo "Awh... num1 is not equal to five";
     }
?>

Ok so pretty much self explanatory but we did use something different. The “==” means “equal”. So we asked if the variable num1 was equal to 5. Check it out in your browser. You should see it say “HEY! num1 is equal to 5!”. Now try changing the variable to a different number and then refresh your browser. Cool, right?

You could also change the “==” to “!=” which means “not equal” or one of the following:

  • > (greater than)
  • < (less than)
  • >= (greater than or equal to)
  • <= (less than or equal to)

That should be enough for now.

You can also keep checking for something with the if statement this way:

1
2
3
4
5
6
7
8
9
10
11
<?php  
     $num1 = 4; //declare the variable to be 5
 
     if($num1 == 5){
          echo "HEY! num1 is equal to 5!";
     }else if($num1 <= 4){
          echo "HEY! num1 is equal to 4 or less!";
     }else{
          echo "Awh... num1 is not equal to five or for or anything less";
     }
?>

LOOPS

Ok now let’s move on to loops. Loops are statements that continue reoccurring until a certain condition is met. We will first look at the for loop. Change your code to the following:

1
2
3
4
5
6
7
<?php  
     for($i = 0; $i <= 5; $i++){

          echo "It is now defcon " . $i . ". <br/>";

     }
?>

Let’s break this one down. The for loop has three parts inside it’s parentheses:

  1. the fist is where you declare the starting point. You set it equal to a variable.
  2. Now you give the condition
  3. And finally, you state the incrementation

The “++” means increment the value by a value of “1″. So every time the loop is run, the variable $i in our case will be increased by 1… so eventually it will hit 6 and the condition “$i <= 5" will return false so the loop will end.

Side note: the
tag is the break tag. It simply skips a line.

Refresh your browser and see what happens.

Ok now that we have that, let’s check out the while loop. Change your code to the following:

1
2
3
4
5
6
7
8
9
<?php  
     $i = 0;
     while($i <= 5){

          echo "It is now defcon " . $i . ". <br/>";

     $i++;
     }
?>

This while loop will give you the exact same result as the for loop. Notice the differences. We now have the three dependencies separated. We declare the starting point before the loop and have the incrementation before the end of the loop.

There are more loops but we will mostly only be using these. Maybe I’ll eventually post a tutorial on more advanced php and introduce those.

Not let’s have some fun. Let’s combine some of the things we learned. Write the following:

1
2
3
4
5
6
7
8
9
10
11
12
13
<?php  
     
     $flower_petals = 8;

     for($i = 1; $i <= $flower_petals; $i++){

          if($i % 2 == 1){
               echo "She loves me!<br/>";
          }else{
               echo "She loves me not...<br/>";
          }
     }
?>

Now run the program and see what happens. Cool, right? But what did I do here? I know this may seem daunting at first blush.. don’t worry though. Let’s break it apart.

  1. So I fist declared a variable.
  2. I started the for loop and instead of having the condition be set to some number, I set it to the variable.
  3. In the if statement, I checked if the remainder of the looping variable $i decided by two was 1. Think about it…if you divide any number by 2, you will always get a remainder of either 1 or 0 and if you divide those numbers in sequence (i.e. 1, 2 ,3, 4 etc) you will have the alternating 1, 0, 1, 0 result.

And that’s it! That’s enough for today but there is still so much more to php! I won’t go into much of the other stuff before we get into wordpress because we won’t need it of building a basic theme. I will, however, teach you how to connect to a database tomorrow and some mySQL syntax.

Play around with what you learned today. Challenge yourself to build something and then if you run into a problem, ask the questions in the comments bellow and I will get to you.

See ya tomorrow!

No Responses to this post

Leave a Reply