16 Nov 2008


PHP For Beginners: Part I

This exercise assumes that you either have PHP installed on your computer already or that you have access to a server with PHP support.

Greetings, eager student! What you are looking at is the first of many tutorials that combine to create an extended resource for PHP beginners. PHP is a staple language within the web design community crucial to the portfolio of any designer. This tutorial and the following series will allow you to read, interpret, modify, and create with PHP, one of the most versatile languages on the net.

Now that we have an idea of what you’re getting into, let’s get a quick overview of what we’ll cover in this tutorial:

  • Identifying PHP tags
  • Writing your first PHP script
  • Combining PHP with HTML
  • Comments within PHP code
  • Quiz

Identifying PHP tags

Web designers have no shortage of languages to use in their work. With CSS, Python, PHP, HTML, and Java crowding the internet with potential, it’s important for the page to know to execute a specific segment of code within a certain language. That’s what tags are for.

If you look in the Source of any web site you’ll see a number of tags. One example is <head>, which is an opening tag used within the HTML language. When the server reads this tag, it knows to execute the following code within the HTML language until it sees the closing tag, which in this case is </head>. The tags used within PHP are very similar to these, and they serve the exact same purpose.

Listed below are these mentioned tags. Look carefully, as they are your first step into the world of PHP programming:

PHP Tags

Pretty basic, right? They all pretty much do the same thing, however your PHP installation will most likely only have the Standard, Short, and Script tags enabled. That’s okay, because Short and Standard are really the only ones that we will be using. Even after you have completed the tutorial, you will see very little deviation from this choice of style.

Writing your first PHP script

Okay, that’s enough background, how about we actually get down to some coding? The script I’m going to introduce you to is called the “Hello world” script. This script has become rather famous as it is almost always the first script given to students when they learn a new language. What we intend to do within this script is simply print the words “Hello world” on your web page. This will be done using the print function.

The print function is exactly what it sounds like: whatever is within the function’s argument, which is whatever is typed after the function, will be printed on the screen for your viewer. See? It doesn’t get much simpler than that. Now that we understand the function, I’m going to give you the piece of code that we will be analyzing. This is because I always have found it easier to understand a language by viewing it and interpreting the coding. So, without further ado, here is the “Hello world” script for PHP:

     echo "Hello World!";

As I’m sure you will notice first, we have used the tags that I referred to in the previous section. I decided to go with the Standard tags so that you would immediately recognize them for what they are. Next, I’m sure you will notice the print function, and following this the function’s argument. What you might not notice, however, are the apostrophes surrounding Hello world. The reason we are doing this is to specify the function’s argument. Without these apostrophes, the print function would try and display the closing tag ?> instead of simply ending with “world”.

So now that you have this script, I want you to do the following, step-by-step, and you will have successfully made your first code.

  1. Copy and paste the above code into Notepad or another web editor such as FrontPage or Dreamweaver.
  2. Save the file as “NAME_HERE.php”. If in Notepad, make sure you are not saving it as “NAME_HERE.php.txt”.
  3. Upload your file to your server (make sure it supports PHP)
  4. Visit the page, and voila! Congratulations!

Now that you have completed the “Hello world” script, try playing around with the script: modify the print argument, change the tags, or try adding another print function and typing some more.

PHP and HTML are very harmonious, meaning that they work well with each other and allow one other to reach new heights. To integrate PHP into HTML is a very simple process. Pretty much all you have to do is copy and paste! Observe below a simple HTML page with our “Hello world” script embedded within.

      <title>PHP for Beginners: Part I</title>
           print “Hello world”;

That’s all there is to it! Explore how this works with your alternate scripts that you created from the previous section.

Comments within PHP Code

Comments are a helpful utility for both the new and professional programmer: they allow the author to write in notes to either themselves or to those who will ultimately use the script in order to assist in the script’s overall interpretation. Whatever is typed within an argument will not be read or executed by the server, making it completely irrelevant to the script’s operation. Below are a few values used to designate the beginning of a comment within the PHP language:

Comments in PHP

Referring back to our PHP and HTML script from the previous section, I decided to add in some helpful comments to you, the reader, so that you can better understand the code:

      <title>PHP for Beginners: Part I</title>
           print “Hello world”;
           // This function will print Hello world

           print “Hello PHP programmer”;
           # This function will print Hello PHP programmer

           /* None of these comments will be seen by the viewer
           unless they look at the source code for the web page*/

Try this concept on your own with your own script. Remember, however, that although comments cannot be seen on the page, they are still visible to the clever user. They simply have to view the page source and they will see the layout of your code, comments included. With that in mind, consider this your warning against doing such things as storing passwords or usernames within these comments, as they will become public knowledge very quickly.


Exams help you to retain knowledge that you have just gained, so with that in mind, I’m afraid it’s back to the classroom for everyone. I urge you to not look back at the material or peek ahead at the answer key.

#1: The print function will…

A.) Display text within the argument
B.) Command the computer to print the viewed document
C.) Not be recognized within the PHP language

#2: The argument of the print function is…

A.) The preceding text to the function
B.) All text following the function
C.) The text following the function within the apostrophes

#3: Comments can…

A.) Be seen only by the code author
B.) Be seen only by the page viewer
C.) Be seen by both parties

#4: Which of the following is not a correct PHP tag?

A.) <?php
B.) <%
C.) <php
D.) <?

#5: Which of the following is not a correct comment tag?

A.) */Comment here/*
B.) //Comment here
C.) #Comment here

Answer key: A,C,C,C,A


Well everyone, that’s it for now! We’ve laid some solid foundation from which to work off of. So with that in mind, keep practicing, good luck, and read on:

Page 24 of 24« First...102021222324