Variables in PHP, just like any programming language, are used for storing values, such as text strings, numbers, or an array of values. Once you have declared, a variable, it can be used over and over again throughout your script.

Variables in PHP must be declared with a ‘$’ symbol. So a variable named test would be declared as $test. The ‘$’ symbol is also needed every time the variable is used after it is declared.

A simple variable declaration should be done as followed :

$test_variable = value;

Now let’s make one variable with a string, and another with a number value :

$text = “Hello World!”;
$num = 10;
  • Note – A string, like the echo statement, can use either single or double quotes, but it must end with whichever it starts with.

There is a lot of freedom when using variables in PHP. First of all you do not have to declare the variable before it is used. The first time you use the variable it will be declared. You also do not have to declare the data type (string, integer, float) of the variable. Once you use the variable, PHP automatically changes the data type to whatever value is stored in the variable.

You can also change the data type of the variable multiple times in your script. Example :

$var = “Hello World!”;
$var = 25;
$var = “Hello World Again!”;
  • Note – This can be done as many times during your script as you’d like.

With all the freedom that comes with PHP variables, there are some rules when it comes to naming them :

  • PHP variable names must start with a letter or an underscore.
  • PHP variable names can consist of only alpha-numeric characters and underscores: a-z, A-Z, 0-9, and _
  • A PHP variable name cannot contain spaces. If a variable is more than one word use an underscore in between them such as ‘$my_variable’ or use capitalizations like ‘$myVariable’.

Now let’s make a simple PHP script that can show us the effects of using variables :

$hello = “Hello World!”;
$fav_num = 23;
echo $hello;
echo ‘ My favorite number is ‘ . $fav_num;

So what’s going on here? First we set a variable to equal the string ‘Hello World!’, then another variable to equal the number ’23′. After that we used the echo command to print on screen what was stored in the ‘$hello’ variable. The second echo statement prints a string followed by the ‘$fav_num’ variable.

  • Note – In the second echo statement, to connect the string to include the ‘$fav_num’ variable, we use a period in between the two. This is known as concatenation, or to concatenate.

When you open your newly made script in a browser it should look something like this :

php script output.

That pretty much covers the basics of PHP variables. Try to write some scripts with what you’ve learned to get used to using variables. A good thing to try also, is naming the variables something that isn’t recommended, just to see what the error looks like that PHP would likely spit out.

As usual if you’ve got any questions, just comment below.

Before we doing any coding, let’s talk about what PHP is. PHP (Hypertext Preprocessor) is a widely used scripting language that is designed for web development and can be embedded directly into HTML.

What makes PHP so different from client-side languages like Javascript and HTML is that PHP code is executed on the server, and then sent to the client as HTML. The amount of things that PHP can do versus HTML and with HTML, make the possibilities of what a web page can do grow exponentially.

One of the strongest features of PHP is its ability to connect to a wide range of databases. Those features will be touched on in later, more advanced tutorials.

This tutorial and any further PHP tutorials will assume that you will be using a server that supports PHP, and that you know HTML. If you don’t know HTML then I strongly recommend not reading this tutorial until you’ve learned it.

Now let’s begin writing your first PHP script.

  • Note – There are several programs you can use for writing PHP, but if you don’t have a specific program such as Adobe Dreamweaver or Panic Coda, you can simply use your computer’s most basic text editor.

First off, create a new file and save it as helloworld.php. Yes the infamous ‘Hello World’. If you don’t know what I’m talking about and plan on learning other programming languages later, then get used to seeing ‘Hello World’ as your first tutorial.

I’ll start by showing you the code, then explaining it piece by piece after you’ve looked through it. I’ve found that’s the best way to work with stuff like this.

Yup, that’s it.

We start by creating a blank html document with all of the standard tags. Only one thing, there’s something new in the body tag.

Whenever you write PHP code, you have to start with “<?php” and end it with “?>”. This lets the server know where the PHP code is within the HTML file.

  • Note – “<?php” tags can be placed anywhere in the HTML document.

The only line within the PHP tag is “echo ‘Hello World’;”. The ‘echo’ command tells PHP to write on the screen whatever text you put within the following quotes.

  • Note – You can use either single quotes or double quotes for ‘echo’, but whichever one you open with, you must close with as well.
  • Note – You can also put HTML code within the quotes following ‘echo’. This will become very helpful later.

The last thing in the line is the semi-colon, which has to be placed at the end of every PHP line, to let PHP know that the command is done, and to move on to the next line. Without the semi-colon, the page would result in an error.

Here’s what your page should look like after opening it in your browser :

First PHP page.

That’s really all there is too it. Thanks for taking the time to read this tutorial and I hope you enjoyed writing your very first PHP script. If you have any questions, please comment below.

In Part 5 of the Easy PHP Guestbook tutorial we will cover retrieving the inserted data from the database and displaying it in a browser.

Create a new file and call it viewguestbook.php, open this file and put the following code in it.

$entriesq = mysql_query(“select * from guestbookentries”);
while($entriesr = mysql_fetch_array($entriesq)){
     $name = $entriesr["name"];
     $date = $entriesr["date"];
     $website = $entriesr["website"];
     $entry = $entriesr["entry"];
     echo “<tr><td>Name:</td><td>” . $name . “</td></tr>”;
     echo “<tr><td>Date:</td><td>” . $date . “</td></tr>”;
     echo “<tr><td>Website:</td><td>” . $website . “</td></tr>”;
     echo “<tr><td>Entry:</td><td>” . $entry . “</td></tr>”;

After you’ve made a few entries using the form from part 3 combined with the added code from part 4, you should be able to run this script and it should display all the entries that were inserted. Hopefully this was helpful to some. In the next few days I will be posting a download that has all these files with all of the source code as well as creating an advanced guestbook that can be purchased.

In Part 4 of the Easy PHP Guestbook tutorial we will cover creating the inserting data into the database that was filled out in the user submission form.

Open the guestbookform.php file from part 3 and put the following code in it at the very top of the file:

     $name = $_POST["guestbookname"];
     $date = time();
     $website = $_POST["guestbookwebsite"];
     $entry = $_POST["guestbookentry"];
     mysql_query(“insert into guestbookentries (name,date,website,entry) values (‘$name’,’$date’,’$website’,’$entry’)”);
echo “Your entry has been submitted”;

This part of the script does 3 things, it checks to see if the submit button has been pressed, if it has then it assigns the values from the submission form to variables, and then takes those variables and inserts them into the database. Finally it will output a message stating that the entry has been successfully entered into the database.

This concludes Part 4 of the tutorial, in Part 5 we will cover how to display the entries that are in the database. Part 5 will also be the last part of the tutorial at which point I will make a more advanced guestbook script along with an admin section to manage entries and will sell it on this site for $5.00

In Part 3 of the Easy PHP Guestbook tutorial we will cover creating the user submission form for our guestbook.

Create a new file and call it guestbookform.php and put the following code in it:

<!DOCTYPE html PUBLIC “-//W3C//DTD XHTML 1.0 Transitional//EN” “http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd”>
<html xmlns=”http://www.w3.org/1999/xhtml”>
<meta http-equiv=”Content-Type” content=”text/html; charset=UTF8″ />
<title>My Guestbook</title>

<form action=”” method=”post”>
<input type=”text” name=”guestbookname” id=”guestbookname” />
<br />
<input type=”text” name=”guestbookwebsite” id=”guestbookwebsite” />
<br />
<textarea name=”guestbookentry” id=”guestbookentry” cols=”45″ rows=”5″></textarea>
<br />
<input type=”submit” name=”submit” id=”submit” value=”Submit” />

Save the file and upload it to your guestbook folder that you created on the server from the previous tutorial. You can point your browser to http://yourservername.com/guestbook/guestbookform.php

You should see the form with a spot to write a name, website and an entry, as well as a submit button. This file as it is now will not perform any functions, in part 4 we will cover how to take the values that a user would fill in and store them in the database.

This concludes Part 3 of the tutorial.

In this part of the tutorial I will cover briefly how to setup a new database and create the php file that will have the database connection strings.

First things first, create a new database and name it guestbook, assign a new user to this database name the new user guestbook, assign a password for this user 12345 will work for now.

Creating databases and assigning users will not be covered in this tutorial, this should be covered in a help file provided by your hosting company.

Once your database has been created and a user has been added you will need to setup the tables that will store each guestbook entry.

For starters lets create a table and call it guestbookentries

You will need 5 columns in this table:

  1. guestbookentryid – Set this column as the primary key and set it to auto increment, the type will be BIGINT
  2. name – The type will be TEXT
  3. date – The type will be DATETIME
  4. website – The type will be TEXT
  5. entry – The type will be TEXT

I suggest using phpmyadmin to setup this table in the database, if you don’t feel up to creating this table you can download this file guestbookentries.sql

You can import that file using phpmyadmin and it will create the table for you.

Once the table has been created you need to create a new file and name it sqlcon.php and place the following code into this file.



//This line of code connects to the mysql server

//This line of code selects the correct database
@mysql_select_db($database) or die (“Unable to select database”);


Upload this file to a new folder on your server, create a folder called guestbook on your servers root and upload the sqlcon.php file to there.

You can point your browser to this file http://yourservername.com/guestbook/sqlcon.php

If the page comes up blank then everything is working as it should, if it comes up with an error then you’ve made a mistake somewhere. I’d be happy to help troubleshoot any problems you are having up to this point.

This concludes Part 2 of the tutorial

Just thought I would share some of my php knowledge with the rest of the world and cover a much asked about topic with a simple step by step tutorial.

This tutorial will be divided into parts starting with how to check and ensure that your server meets the minimum requirements to host a guestbook.

To start with you should make sure that you have a host that supports php as well as mysql.

Here’s a quick way you can check if your server supports php and mysql

Create a new file and call it phpinfo.php and place the following code in it.


Upload this file to your servers root folder and point your browser to it, so for example if I uploaded this file into my servers root folder I would point my browser to the following.


First, if your server supports php, then it will run this file and output information related to your php configuration, search for mysql on this page, if its there then the server supports it. 

This concludes Part 1 of the tutorial.