PHP (which, oddly enough, stands for PHP: Hypertext Preprocessor) is what is known as a server-side scripting language. With html all the web server has to do is display the page. With PHP, the web server has to actually process the page and may have to do additional work before it can display the page. You, for example, write code that asks (in PHP language) the webpage to display your name. The server, which has PHP installed, interprets the code that you write and, if you wrote the code correctly, displays your name.

Getting started with PHP 

The only way to process a PHP file is via a web server that is properly set up to run PHP; ie. PHP files will not behave properly in a browser on your computer [footnote]unless you set your computer to function as a de facto web server [/footnote] so you will have to upload them to the server in order to see if they worked correctly or not.

PHP: Strings

  1. To begin, create a new html file with our friend, the HTML skeleton extension, and save it as intro.php.
  2. Click to the right of the <body> tag and press return a few times to create a work space. Now enter the following code to start a PHP segment:

    press return a few times and type


    to end the segment. This is your work space. Everything you do in PHP has to take place between the <?PHP and ?> tags in order for the server to interpret your code.

  3. On the first line after <?PHP type: echo “your name”;
  4. Don’t be literal here, put in your actual name. The echo command tells the server to display whatever comes after it. The quotations are used because you are displaying a name instead of a number. Anytime you enter a word instead of a number you are entering what is known as a string. The semicolon is used to mark the end of a command statement. Without the semicolon you would get an error message.
  5. Save your file. Upload the file to your server and test it out by going to Did you see your name? Great. If not, ask your instructor for assistance. Next, we will examine a new, and very important, concept known as variables.

Editing directly from the server

As you may have already figured out, it’s a real pain to have to constantly re-upload a php file every time you need to test it.The solution to this problem is to edit the php file directly from the server. Here is how it works:

  1. Screen Shot 2015-03-19 at 12.20.19 PMOpen Cyberduck and click the preferences option shown here.
  2. Click the Editor tab and choose Brackets as your editor. Chances are you will have to browse to the Applications folder in order to choose Brackets.
  3. Click “Always Use this Application”  and close preferences.
  4. Find your way to intro.php and click gently on it just one time to select it. At this point the Brackets edit icon should appear in the top navigation, next to the Refresh icon.
  5. Click the edit icon and the online version of intro.php should open in a new Brackets window. Whenever you make changes to this version of intro.php and save it, the file on the server will automatically update. All you have to do to see the most recent results of your work is refresh intro.php with your browser.
  6. Don’t forget: when you are done editing your file in Brackets, close it. Each time you come back to a file after logging off, turning your computer to sleep, etc, the file can disconnect from the server.

 PHP: string variables

  1. Now that you have the server version of intro.php open in Brackets,  delete the echo “your name”; statement (ie. everything between the <?PHP and ?> tags. Add a new line that says
     $myname="your actual name"; 

    Congratulations you have just created a variable. Because $myname is tied to a word instead of a number, it is a type of variable known as a string variable.

  2. Now add a second line that says
     echo $myname; [footnote]Don't forget the semicolon.[/footnote]
  3. Save the file and test it by in your browser. Once again you should see your name. The difference is that this time your name is tied to a variable.
  4. Change $myname=”your name”; to

    Save and test again to see if the change took effect.

PHP: numeric variables

  1. Now let’s play a little bit with numbers. Delete everything between <?PHP and ?> but leave some work space. Now type:
    echo 15*30;

    Save and upload the file again. The number 450, which is the product of 15 times 30 should appear.

  2. Delete echo 15*30; Now enter: $firstnumber=15; and on another line: $secondnumber=30;
  3. You have just created two numeric variables which behave like (not surprisingly) numbers. On a third line enter: echo $firstnumber * $secondnumber;
  4. Save and test everything once again. You should get the same answer as before.
  5. Modify the line that says echo $firstnumber * $secondnumber; to say $thirdnumber=$firstnumber * $secondnumber; This assigns the product of $firstnumber and $secondnumber to a new variable called $thirdnumber. Now echo echo $thirdnumber; and check your result.


You can do a number of useful things with strings, numbers, and variables and one of them is a process called concatenation which allows you to tie numbers, variables, and strings together. Concatenation sounds intimidating but the process is easy to conceive if you think of it as something like a lego set for sticking words, numbers and variables together into various combinations. In this case the “glue” is nothing more than a dot as in . symbol. Lets see how it works.

  1. While you still have those $firstnumber and $secondnumber variables let’s put them to use. Delete the statement $thirdnumber=$firstnumber * $secondnumber; and replace it with this line:
    echo "The first number is ".$firstnumber.", while the second number is ".$secondnumber; [footnote]Make sure the quotation marks are exactly the same. [/footnote]

    What you are doing here is combining two phrases (strings) with the values of two variables to create a new, more complex statement. When you save and test you should see the following statement: The first number is 15, while the second number is 30.

  2. Change the values of $firstnumber and $secondnumber to something new and test again. The new numbers should appear in the statement.
  3. Your statements can also incorporate html code. For example, a string can include which will cause a line break when it is executed by the server. Lets try it by modifying the statement that you entered previously to say
    echo "The first number is ".$firstnumber."<br/>"; echo "while the second number is ".$secondnumber;

    When you test it this time the second statement should appear on a new line.


One of the most powerful features of PHP or any other scripting language is the ability to ability to execute a statement multiple times by using a technique called looping. There are several different types of loops but all have one important feature in common: they repeat an action (or statement) as many times as they are instructed. The example you are about to see is known as a while loop because it executes a statement as long as another statement is true. Enough talk, let’s see an example.

  1. Delete everything between <?PHP and ?> and enter the code shown below.
    $loopnum=0; while ($loopnum<3) {echo "the current value of loopnum is ". $loopnum."<br/>";$loopnum++; }

    The first line initializes a new variable called $loopnum and sets its value to 0. $loopnum is how you will control your while loop. The subsequent lines perform the loop three times while the $loopnum variable is less than 3 and should produce the output shown at right. If you change the value in the line that states while ($loopnum<3) to a higher or lower number, the output will increase or decrease accordingly.

the current value of loopnum is 0
the current value of loopnum is 1
the current value of loopnum is 2

To summarize, loops are well worth knowing because they can save you a tremendous amount of work. We will revisit them later but, for now, it’s most important for you to know that they exist.


Many websites built with PHP instead of html take a modular approach to building webpages via a feature called includes. Instead of one big file (as in html) that constitutes one webpage, pages constructed with PHP often are built “on the fly” out of multiple PHP files. You may have noticed this already while working through the WordPress lessons.

The beauty of this approach is that it works really well for updating a large website. Includes allow you to store common features such as headers, footers, and links in individual files that are “included” (ie. shared) among all of the different pages of a website. Imagine, for example, that you create a series of pages that do not have links built into them. How do you link all of the pages of your site together? Simple, you create one file that links to each of your site’s pages and “include” it into each of the pages. Here’s how it works:

1. Download from and unzip it into your downloads folder.

2. Use your ftp client (cyberduck most likely) to drag the includes folder into your public_html folder. 

3. Take a look in the folder. You should see five php files and one CSS file. 

4. Open one.php in your browser by browsing to whatever your domain is and appending includes/one.php as in All you should see is a big red 1 and no links. Now open links.php in your browser. All you see there are links with no content. Let’s change that and combine links.php with one.php by including it. 

5. Open one.php with your editor and look at the code.  Just below the body tag, type or paste in  <?php include ‘links.php’; ?>. Save one.php and open it in your browser. It should now link successfully to two.php, three.php, four.php and itself. Problem is, none of the other files have links yet.

6. Open two.php, three.php, and four.php, and, once again, insert <?php include ‘links.php’; ?> just below the body tag. 

7. Test everything again. This time all four files should be linked. 

8.Take this one step further. Open links.php and create a new link to or some other website. Save links.php and test one.php etc. again in your browser. You should see the new link in all four files but you only changed ONE file: links.php. Even so, that new link to cnn appeared in all four files because you added to links.php which is now part of all four files. As you can see, this is an effective strategy for creating links to multiple pages without having to update the links multiple times every time you add or change a link. 

Next: More PHP. Understanding Arrays