PHP programming tutorial




      1. Introduction

      PHP is a popular programming language that is used all over the web to produce many of the rich web applications that you use on a daily basis. It is languages like PHP that have transformed the web as it used to be; a collection of static pages, into the interractive and engaging experience that it is today. PHP at it’s simplest can be used have the results of a web form e-mailed to you, or at it’s most complex can be used to create vast interractive web applications that rival what was once only possible in a stand-alone desktop applications. PHP and similar languages have had a profound effect on the way the web works and have enabled many of the new ‘Web 2.0′ services that now exist. We are gradually moving towards a point where all software is written to run inside of a web browser.

      This tutorial is intended to help the novice programmer learn the basics – you don’t need to have previous programming experience, but a basic understanding of HTML is assumed. You don’t need to be an HTML guru to start working with PHP – even if you’re used to doing most of your HTML work in a software package like Dreamweaver or Frontpage, you probably will still have picked up enough HTML knowledge to get started using PHP.

      Getting started with PHP

      In most cases, PHP code runs on a web server – this means you’ll need to either buy a web hosting account or install a web server on your own computer before you can really start experimenting with PHP. We recommend purchasing a budget web hosting account as the easiest option to get started – with this you’ll also receive a certain level of support which can be invaluable if you’re not sure if you’re doing something right. Scroll to the bottom of this article for some information on the affordable options available from Digital Crocus.

      So how do I create my first PHP script?

      The first thing you will need is a text editor – any plain text editor will do; some people use ‘notepad’. However, we reccommend that you use a text editor with syntax highlighting – what this means is the text editor will colourise your code as you write it to make it easier to read and understand. Syntax highlighting also makes it very easy to see when you’ve made a basic coding error because the colours just won’t look right. Digital Crocus recommends one of the following editors which support syntax highlighting:

      There’s many more and a simple Google search for “text editor syntax highlighting” will give you a good selection.

      So now you’ve got a good text editor, how do you create a PHP script? Well, it’s quite simple really – PHP’s interpreter allows you to mix HTML code and PHP code within the same file. In fact, any plain HTML file can simply be renamed to end in .php rather than .html and that is a valid PHP script. Consequently, probably the simplest PHP script you can write looks something like this:

      1.  
      2. <html>
      3. <body>
      4. Hello World!
      5. </body>
      6. </html>
      7.  

      If you put that in a file called helloworld.php, upload it to your web server and then visit it in a web browser, you should see the words ‘Hello World!’. Well I know that’s not very exciting, but it illustrates the point that a PHP script is basically just an HTML page with code added (or in this case, no code added!)

      How do I add code?

      Let’s start by explaining a little bit about how the PHP interpreter parses your files (parsing is the act of reading and making sense of a piece of text). As with all programming languages, PHP is read and acted-upon in the order it’s written, starting at the top of the file and progressing through it line by line from left to right until the end of the file. You tell the PHP interpreter that you want to start writing PHP code using the <?php tag, and that you want to end PHP code and return to HTML with the ?> tag. Here’s a basic practical example:

      1.  
      2. <html>
      3. <body>
      4. Hello World!<br />
      5. <?php
      6. echo "Hello from PHP!";
      7. ?>
      8. </body>
      9. </html>
      10.  

      Here you can see the same “Hello World!” example above but this time with some PHP code inserted. As you can see the PHP code falls within <?php and ?> tags – in this example there is only one PHP statement – this statement tells the PHP interpreter to output the text “Hello from PHP!”. In simple terms, a statement is a single instruction to the PHP interpreter to do something. Each statement is usually on a line of its own and is always followed by a semi-colon – this is called the statement terminator and lets PHP know that you have finished a statement – sort of like the full-stop on the end of a sentence.

      You should also notice that the words “Hello from PHP!” are written inside double-quotes – this is called a string, whenever you want to place a piece of text into your PHP code it must always be enclosed in either single or double-quotes (we’ll discuss the differences between the two in the next chapter).

      Another thing you should notice is that we’ve indented the code – each time we start a new logical block, we add another tab (some people use tabs, others use spaces). This isn’t necessary for the code to work, but it does significantly improve code readability and you are strongly encouraged to follow this convention in your own code. This applies equally well to HTML as it does to PHP – you can see that both the PHP and HTML code are indented. This is a convention that we’ll follow throughout the tutorial so you can pick up proper indentation by observing how it’s done here.

      One final thing to note is that you can see syntax highlighting in practice in the code fragment above. If you followed our advice and installed a text editor that supports syntax highlighting, you should see similar colouration in your code. Don’t worry if the colours aren’t exactly the same, each text editor uses a slightly different array of colours for different parts of the code – the important thing is to become used to how code is supposed to look in your chosen text editor.

      Next chapter… (2. Variables)