PHP - The Basics
Written by Owen Jones
In this first tutorial I’m going to explain how to create PHP files, how to print text, and what variables are.
PHP – What is it?
When written in 1994 PHP originally stood for ‘Personal Home Page’. Written by Rasmus Lerdorf to replace a small set of Perl scripts he used on his site. Nowadays PHP stands for ‘Hypertext Pre-Processor’. It is a robust dynamic coding language and alongside ASP, JSP and RoR provides the ability to include dynamic content onto the web. PHP can be installed on Windows, UNIX and Solaris systems and can be installed as a module to apache.
But enough of the history lesson, let’s get coding!
Quick Tip – Creating PHP files –
- To create a PHP file open up your favourite coding software. I personally use Dreamweaver but you can use anything you want.
- If it’s possible turn syntax colouring on and set your current language to PHP.
- When you are done, save the file as <file-name>.php (If using Notepad change ‘Save as Type’ to ‘All Files’).
- Using an FTP client, upload the file to your web host.
NB: A web host with PHP enabled for your account is required.
You can also end the filename with .php3, .php4 or .php5 which, if your server is correctly configured, should run that script in the version of PHP the file extension specifies, but you shouldn’t need to do this for the examples in this tutorial.
First Stop – Opening and Closing Tags
One of the most important things when learning PHP is opening and closing tags. These surround your PHP code, which will not run outside them.
There are quite a few sets of opening tag; I’ll show you three of them.
The good ol’ standard tags. Simply <?php and ?>
- The good ol’ standard tags. Simply <?php and ?>
- Short Tags. <? And ?>
- ASP Tags <% and %>
Please Note: Short Tags and ASP-like Tags must be enabled in your PHP settings. They are not enabled by default. Contact your web hosting provider to do this.
Each line between these two tags should end in a semi-colon (;), unless it’s not a statement. I will explain this in more detail in the next few tutorials, so don’t worry if you don’t completely understand what’s going on.
Now, down to the Nitty Gritty! Unlike most PHP tutorials, I’m going to make a slight detour and before we cover printing text I’m going to cover Variables!
If you have ever done any algebra you should hopefully be at ease with variables. A variable in algebra is a letter that will represent a value. Every time you use this letter it gets replaced by the value assigned to it. Variables in PHP are very much the same. You assign a value to the variable, and, like in algebra, the variable name gets replaced with the value.
Unlike many other coding languages, in PHP variables do not need to be defined before they can be used.
In PHP variables are used like so:
<?php
$my_variable = ‘I am a variable’;
?>
There I have assigned the text ‘I am a variable’ to the variable ‘my_variable’.
Variable names must be Alphanumeric (a-z, 1-9) but may include underscores (_) variables must start with the dollar sign ($) to tell PHP this is a variable.
Some examples of valid variable names are:
$my_variable
$_thevar
$my_name_is
And some not so valid:
A_variable (No dollar sign)
$the’-‘variable (Illegal Characters)
It’s all good having a variable, but on its own it isn’t much use. Here’s how to print Data to the browser.
Printing information to the browser is very important. What’s the use of doing all this work if you can’t see the results? There are a few ways to serve up text. I am going to show you the easiest way. Print and Echo.
Print and Echo are PHP functions. I’m not going to explain what that means until the next tutorial so you might not understand exactly what they are.
To print something to the browser simply pass what you want to say to either print or echo, like so.
<?php
Echo (“This is what will be printed”);
Print(“This will also be printed”);
Print(“I am printing a variable - $my_var”);
?>
Both of these are valid ways to do it and there is very little difference between the two. Please note that the text is contained in double quotations. This enables us to place the variable into the text. There is another, more complex way to do this which I may explain later.
That concludes the tutorial.
A brief example of what you should now be able to achieve is:
<?php
$my_colour = ‘Orange’;
Print(“My favourite colour is: $my_colour”);
?>
This will print ‘My favourite colour is Orange’ to the browser.
In the next section:
Comments, Arrays, Loops and Switches. Thanks for reading. If you have any problems or questions don’t be afraid to post a comment below.
Owen Jones is an Advanced PHP coder. Find out more about him @ http://owenjones.net
Tags: Basics, Examples, First Post, How to, Jones, Owen, PHP, Quick Tip, Tutorial


Nice tutorial Lewis! Glad to see things are getting a little more advanced!
One note though: “echo” is not a function, it is a PHP construct - so you are not required to use brackets with it.
E.g.
echo "Hello World";a. I wrote the tut :p
b. I know you don’t need the brackets. I never use it with the brackets, I just didn’t want to confuse anybody at this early stage.
Noticed you wrote the tut right after I wrote the comment….sorry…
Post