Monday, 24 September 2007 12:22

Vamp the Linux LAMP with PHP


One key driver of free and open source software is the LAMP paradigm – cleverly named after the systems it embraces – Linux, Apache, MySQL and PHP. This powerful combination delivers high powered data-enabled web and intranet sites with a minimum of fuss. Here’s how to get started with PHP.

PHP is a web scripting language (akin to Microsoft’s Active Server Pages or Adobe’s ColdFusion.) Originally, it began as a humble collection of Perl scripts designed to ease the creation of popular web addons like guest books and message boards, PHP meaning simply “Personal Home Page.”

PHP is now in its 5th version and has received massive reworking and updates since its unpretentious beginnings, becoming a solid platform for building web sites at all levels. It provides basic interactive features through to dynamic enterprise applications. PHP is the most obvious starting point for learning how to work with the LAMP environment because it is the most visible component. PHP pages make up the user interface that people will interact with (whereas Linux and Apache provide the underlying engine, and MySQL gives database storage.)

Even if you do not actually run a Linux environment yourself, chances are your web host gives you PHP (and MySQL) for free. This is true of paid web hosts as well as any complimentary free web space that your ISP gives.

In fact, it’s well worth getting familiar with the basics of PHP just to enhance your own personal web page. It’s also worthwhile so you can debug or at least make sense of PHP pages you encounter. You’ll find it dead easy even if you’ve never programmed before.

Entering PHP mode
PHP code is interspersed with standard HTML tags. The web server – Apache, mostly, but PHP pre-processors can be added to IIS and other servers – receives a request from a web browser as usual. It picks up the PHP page and executes the programming commands inside, rendering any generated output along with the HTML tags. This final output is returned to the web browser, giving the illusion of an ordinary, static web page despite the fact it was generated right on the fly by the server.

The pre-processor fires when the web page suffix is .php instead of the regular .html (or .htm). It then looks inside the file for special tags which indicate where literal HTML output stops and PHP code begins. There are four possible tags that can be used, all achieving the same purpose:

<script language=”php”

When the pre-processor encounters one of these tags, it stops recognising text as HTML to be returned as-is and instead sees it as program code to execute. This continues until a closing tag is found, at which point the pre-processor just emits subsequent text as straight HTML again.

The closing tag must match the opening tag; use ?> for either of the first two options above, %> to close <% and </script> to match <script language=”php”>.

A variable is a data item that can have a value assigned to it. Over the course of the program you may alter this value and use it for display or in calculations.

Some languages are strict about how variables are used. If you specify it has integer values only you can’t then store a decimal number or a text string in it. PHP is different, and this is one of the reasons it is generally considered a fast language to work in. In PHP, a variable begins with a dollar sign ($) and that’s it; you do not explicitly state any restriction on the data it can hold. Instead, PHP will work out the data stored by how it is used and what’s more, it’ll go out of its way to help you work with different types of data.

Let’s put these together to make a real working PHP page.

Type up the following in a text editor and save it to disc with a .php file extension, perhaps as testing.php. To view it from the web, you’ll need to make sure you save it into an area covered by your web server.

<p>10 + 20 =
  $a = 10;
  $b = 20;
  echo $a + $b;
<p>10 + “20” =
  $c = “20”;
  echo $a + $c;

There’s two different ways you can take this for a whirl. The first is, as said, to copy the file into your web server directory then call it from a web browser as you would any other page except be sure to specify the full filename including .php suffix.

Alternatively, you can invoke the PHP pre-processor directly from the command line by executing php testing.php – in this case the HTML output will still be generated but will display to the screen.

Either way, you’ll see the output tells you twice what the sum of 10 and 20 is. Compare the output with the program code, reading from top to bottom. You can see we switched into PHP mode twice, with HTML tags used to control the paragraphing and messages.

You’ll also note the differences between the two additions; the first time, two variables called $a and $b are assigned numeric values. Their sum is displayed. The second time around, $a is added to $c which actually contains the text string “20”, rather than the numeric value 20. When the addition is performed PHP sensibly recognises the need for “20” to be interpreted as a numeric value and still delivers the correct result. PHP offers other mathematical operators as well as addition, namely subtraction (-), multiplication (*) and division (/).

There are some more advanced operators too which perform functions like bitwise operations and modulus division.

You might actually want PHP to join two text strings together. In that case, don’t use the + symbol because PHP interprets that as adding numbers. Instead, use a period (.) to join two strings together, one right after the other.

There’s two more import things to note in the listing above. Semicolons (;) are used to separate commands in PHP and the echo command, which as you might guess, sends the result of a calculation or some other operation to the output being built up.

Conditional statements
Sometimes you might want to display a message or perform an action only if a certain condition is true. You don’t want this action to happen if the condition isn’t met. The simplest way to achieve this is with the if statement.

if (condition) {
  Perform some action
  which can be as many lines as needed
else {
  Optionally perform some other action
  if the condition is not met

This looks easy enough, but actually there’s a number of important things to be aware of.

Firstly, the condition can be expressed by comparing variables to other variables or to literal values using operators, some of which are well known and others which are not. The less-than symbol (<) is straightforward enough; a condition like ($a < $b) clearly tests if the value of variable $a is less than that of variable $b. Similarly, greater-than-or-equal-to is easily understood; ($a >= $b) tests if $a has a value which is either greater than, or equal to, that of $b.

What is not so obvious is that values are tested for equality using a special operator ==. This is because the equals sign (=) is already used to assign values to variables, so something else was needed to check if two items are the same. Remember, $a = $b copies what’s in $b over to $a. ($a == $b) tests if $a and $b both have the same value already.

You should also note the special symbols used. A pair of parenthesis surround the condition. Curly braces are used to define just how many program lines should be executed as part of the action. Also, you will see an “else” clause which lets you do something special even if the condition failed. An obvious example might be to display different status messages like so:

if ($loginSuccessful) {
  echo “Logged in.”;
else {
  echo “Log in failed. Please try again.”;

This else clause is purely optional. There is no need to include if it is not required. In this case, PHP will do what it is instructed if the condition is met but will not do anything special if the condition fails; it will just keep executing instructions at the closing curly brace.

There are three fundamental constructs in programming: sequence – where programs execute lines one at a time, in order; selection – where a condition is evaluated as above; and repetition – where a block of lines is repeatedly executed over and over. This latter construct is what we call a loop.

PHP lets you loop a series of instructions over and over, in several different ways. The most basic of these is the while loop which just keeps running forever so long as a specified condition remains true. As soon as the condition becomes false the loop will end.

$a = 0;
while ($i <= 10) {
  echo $i;

This bit of PHP code keeps running the loop so long as the value of $i is not yet greater than 10. Each time through the loop two commands are executed. The first displays the current value of $i, and the second increments $i by one. A special operator is used; ++ means simply that PHP should add one to the variable. This is a shorthand way of writing $i = $i + 1; which would also be valid and perform the same job.

Here’s where we’ll stop for the moment; check out part two to see more about PHP including getting user-entered data out of HTML forms. Also be sure to check PHP.Net for downloads, tutorials, articles and documentation.

Hopefully you're encouraged to try your hand at making your own PHP page. You'll be impressed at the results and how easy it is to obtain them. Before you know it, you'll be reading and writing PHP like a pro.

Subscribe to ITWIRE UPDATE Newsletter here


The much awaited iTWire Shop is now open to our readers.

Visit the iTWire Shop, a leading destination for stylish accessories, gear & gadgets, lifestyle products and everyday portable office essentials, drones, zoom lenses for smartphones, software and online training.

PLUS Big Brands include: Apple, Lenovo, LG, Samsung, Sennheiser and many more.

Products available for any country.

We hope you enjoy and find value in the much anticipated iTWire Shop.



iTWire TV offers a unique value to the Tech Sector by providing a range of video interviews, news, views and reviews, and also provides the opportunity for vendors to promote your company and your marketing messages.

We work with you to develop the message and conduct the interview or product review in a safe and collaborative way. Unlike other Tech YouTube channels, we create a story around your message and post that on the homepage of ITWire, linking to your message.

In addition, your interview post message can be displayed in up to 7 different post displays on our the site to drive traffic and readers to your video content and downloads. This can be a significant Lead Generation opportunity for your business.

We also provide 3 videos in one recording/sitting if you require so that you have a series of videos to promote to your customers. Your sales team can add your emails to sales collateral and to the footer of their sales and marketing emails.

See the latest in Tech News, Views, Interviews, Reviews, Product Promos and Events. Plus funny videos from our readers and customers.


David M Williams

David has been computing since 1984 where he instantly gravitated to the family Commodore 64. He completed a Bachelor of Computer Science degree from 1990 to 1992, commencing full-time employment as a systems analyst at the end of that year. David subsequently worked as a UNIX Systems Manager, Asia-Pacific technical specialist for an international software company, Business Analyst, IT Manager, and other roles. David has been the Chief Information Officer for national public companies since 2007, delivering IT knowledge and business acumen, seeking to transform the industries within which he works. David is also involved in the user group community, the Australian Computer Society technical advisory boards, and education.

Share News tips for the iTWire Journalists? Your tip will be anonymous




Guest Opinion

Guest Interviews

Guest Reviews

Guest Research

Guest Research & Case Studies

Channel News