Write a comment on this article
Are you ready to create your first page with PHP? Ok, let's do it!
Writing your PHP file is quite easy, all you need is a simple text-editor like Notepad on Microsoft Windows. Start your preferred editor and you are ready to begin. We will now create a valid HTML file which can be viewed with any webbrowser.
Firstly, we create the HTML structure without any PHP code. This gives us the basis to build on. If you are not familiar with HTML yet, you may want to read an introduction to HTML first. Here is the code which produces a table with 2 columns and 3 rows:
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>My first PHP page</title>
</head>
<body>
<table border="0">
<tr>
<td>Number 1:</td>
<td width="50" align="right">5</td>
</tr>
<tr>
<td>Number 2:</td>
<td align="right">9</td>
</tr>
<tr>
<td>Result:</td>
<td align="right"><strong>14</strong></td>
</tr>
</table>
</body>
</html>
This will give us the following output:

Copy the code from above to your editor and save the file as "My_first_PHP_page.php". It's important that the file-ending is .php, so the webserver will recognize it as a PHP-file.
Now we will edit the file and add some PHP code. PHP code must be inside PHP tags, which are <?php and ?>. In our HTML file we have three numbers: number 1, number 2 and the result (which is obviously the sum of number 1 and 2). We calculated the result manually (with our brain), which is not very difficult with this operation, but as you can imagine, calculations can become very complex and may depend on user input (which we cannot predict) or database queries. So we want PHP to do what we did manually.
At the beginning we define variables, assign the correct values and do the calculation. Then we print the result. We will need 3 variables, one for number 1, one for number 2 and the third for the result. So here we go:
$number1 = 5; $number2 = 9; $result = $number1 + $number2;
As you can see, the sum of the variables $number1 and $number2 are assigned to variable $result. Of course, it's not only possible to sum up two numbers. Nearly any mathematical calculation can be done with PHP. Although PHP is rarely used to solve mathematical problems, this is just a simple example with a simple calculation. You can browse through the tutorials to find more complex examples if you are getting bored.
So we have our nice HTML structure and this simple PHP code, let's combine them. You can paste the PHP code nearly anywhere into the HTML structure, even at the very beginning. It's just important to keep the correct order of the PHP commands because they will be executed line by line.
<?php
$number1 = 5;
$number2 = 9;
$result = $number1 + $number2;
?>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>My first PHP page</title>
</head>
<body>
<table border="0">
<tr>
<td>Number 1:</td>
<td width="50" align="right">5</td>
</tr>
<tr>
<td>Number 2:</td>
<td align="right">9</td>
</tr>
<tr>
<td>Result:</td>
<td align="right"><strong>14</strong></td>
</tr>
</table>
</body>
</html>
The last step is to print the values of our variables inside the HTML strucutre. This for we can use the command echo. Instead of typing the result manually, we now use PHP code.
<?php
$number1 = 5;
$number2 = 9;
$result = $number1 + $number2;
?>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>My first PHP page</title>
</head>
<body>
<table border="0">
<tr>
<td>Number 1:</td>
<td width="50" align="right"><?php echo $number1; ?></td>
</tr>
<tr>
<td>Number 2:</td>
<td align="right"><?php echo $number2; ?></td>
</tr>
<tr>
<td>Result:</td>
<td align="right"><strong><?php echo $result; ?></strong></td>
</tr>
</table>
</body>
</html>
If you open your PHP-file with your webbrowser you will notice that the PHP code might not be executed. Your page will look something like this:

What happened? Nothing!
As you opened the file locally, the browser received the PHP code as well, but because the browser cannot interpret the PHP code, it just ignores it. As we know, the PHP code must be executed before the browser receives the file. This is normally done by a webserver. So we need a webserver with PHP enabled to test our cool PHP file.
And here comes the harder part: how do we get one? There are several possibilities:
You can install a webserver and PHP locally on your computer. This is not the easiest work but you don't have to connect to the internet to test your page. There are many manuals and documentations online on how to setup a local webserver. If you are ready, copy your PHP file to your webservers document root and call it via your browser: http://localhost/My_first_PHP_page.php. Do not open the file with a double click, you need to open it through your webserver.
LAMP is short for Linux - Apache - MySQL - PHP. Generally, it's a package which provides all you need for simple web development: Linux as operating system, Apache as webserver, MySQL as database and PHP as scripting language. In the internet you can find different versions and live CD's. Once your LAMP is running, copy your PHP file (e.g. from a usb stick) to the webservers document root and call it via your browser: http://localhost/My_first_PHP_page.php.
There are many service providers where you can get webspace with PHP for free (see resources below for links). These services are mostly combined with advertisement and provide little webspace only, but it should be enough to test our file. Once you've registered with such a service, you'll receive your login information to upload files to your server, and you'll get the address of your site. Upload your PHP file to the server and open your site (something like http://www.freehostingprovider.abc/yoursitename/My_first_PHP_page.php) with your browser.
If you have an own domain and webspace with PHP enabled you can copy your PHP file to your webserver and open your site with your browser: http://www.yourdomain.com/My_first_PHP_page.php.
In any case, the result will look something like this:

As you can see the result is not different to figure 1, the HTML structure. But this time, the calculation has been done by PHP, although there is not difference in what the user sees. Congratulations! You've just created your first page with PHP!
You can view this example online: My_first_PHP_page.php. Now it's time to try some more complex examples. You can find some at the tutorials.
PHP, install, setup, testing
If you have a question or a problem and you're looking for a solution, please use the forum. Comments might not be answered.