Welcome to part 3 of my 10 part series on PHP. In the first
two parts I introduced you to the language and to what
software you needed to run it.
In this episode we will look at some simple PHP syntax, and
we'll write a couple of small scripts to get our feet wet,
and get a feel for the language.
What does a PHP script look like?
PHP traditionally is embedded into HTML code within a web
page, as this was its initial intent. However it's becoming
increasingly more popular for web application authors to
write and generate the HTML using PHP from a page that is
made entirely of PHP code.
The following two examples of the world renowned hello world
program should help to show the difference:
Embedding in HTML
<title><?php print "My First Script"; ?></title>
print "<h1>Hello World</h1>";
My personal preference is embedding the PHP code within the
HTML code, however there are plenty of alternative theories
out there, and I would encourage you to explore and develop
your own style of writing scripts.
As an example if you ever decide to work with the Typo3
framework (http://www.typo3.org/) then you'll be highly
likely to use the Full PHP approach. For the remainder of
this series we'll be using the embedded HTML approach.
The first script line by line
As you can see most PHP statements are included in special
PHP tags which for the most part are no different to regular
Because of this, we can freely use them inter-changeably
anywhere you would use any other HTML tags.
Looking at the title line in the first example above, you
can see that we generate the title using PHP code and a
static string. You could use a variable, but since we're
not covering them until next time I'll skip over that for
So, what can I put in these PHP tags?
Well, anything that's valid PHP can go in there, print
statements, function calls, variable assignments and so on,
as an example using the print statement:
We are printing the output of the date function directly
into a set of <H1> tags, which in this case is a
string ready to be printed. Some functions however return
different types such as arrays and resources, the good news
is that in most cases PHP will try to format the output and
display something useful.
For arrays however (We'll cover arrays in more detail in
future article in the series) you'll likely want to use the
print_r routine, for example:
The small snippet above will display the contents of the
POST array, something which can be very useful when testing
forms of data. Simply create a PHP page with just the above
in, and set it as the action in your HTML form tag, and hey-
print_r will list the array items in sequence,
and if wrapped up in <pre> tags will also lay the
output out neatly line after line, as though it was being
output to a terminal or command line.
One last useful tip that often aids in debugging is to
output the phpinfo call, EG:
This provides a wealth of information about the environment
PHP is running in, and can greatly help when first setting
up your server, or when you need a quick reference of what
extras you have installed.
For now we'll leave it there, but I would encourage you to
read the online PHP Manual.
Have a look at each of the function calls and see what they
each return. Customize the script above to output different
pieces of information.
In this episode we looked at our first script, and examined
the ways in which it could be written, in the next episode
we'll be looking at variables, the life blood of any program
not least PHP, and remember don't be afraid to experiment,
for the most part unless your playing carelessly with file
functions, there's no damage you can do, one of the best
places to learn is the PHP web site and the user code