A number of Australian employees of Hewlett-Packard are facing the loss of their jobs as the global computer giant looks to slash its worldwide workforce by up to 30,000.
read more
David M Williams
Monday, 01 October 2007 13:38
Last time we showed how anyone can get started writing, or reading, web pages coded with PHP, the “P” in “LAMP” (the rest being Linux, Apache and MySQL.) We received kind reader feedback that this was the friendliest and simplest PHP introduction they’d seen. What’s best of all is PHP is for everyone; even if you don’t run Linux, you can use PHP on the web space provided by your ISP.
Now to take it to the next level. So far, we’ve showed how to switch from HTML into PHP mode, how to perform basic mathematics and how to alter the flow of the program via conditional and looping constructs. These are foundational concepts but by themselves are not ultimately of great value: what’s missing is user input.
Here’s how to prompt the user for information through standard HTML forms, and then have PHP act on these. Before we can get to that, though, it’s important to understand arrays.
Arrays
An array is an indexed collection of variables which share the same name. This means instead of having two variables called, say, $a and $b, you might have two variables called $a[0] and $a[1]. These two variables are separate; if you change the value of one the other is not affected. You can retrieve the value later and it will still be as expected no matter how much else you have done to other elements in the array.
There are two main benefits of using an array. The first is that you can use a loop to perform an operation on a whole bunch of data items with a very small amount of program code. This works by using another variable to act as an index into the array; each time through the loop increase the value of this variable. To act on a large set of individually named variables would be a more complex piece of program code, and would not easily lend itself to differing numbers of variables.
Additionally – and this is something particularly nice about PHP – the index into an array doesn’t have to be an integer number; it could be a text string. This means it is dead simple to (for example) hold a count of the number of times different words are used in a sequence of text; all you need to do is have an array which is indexed by the actual word itself, and which simply holds an increasing count. Most other languages don’t have this flexibility and only allow arrays to be indexed by an integer.
Here’s what some real code looks like which implements these concepts, including how to actually define an array:
<?php
$arr = array(10, 20, 30, 40);
$counter = 0;
while ($counter < 4) {
$counter++;
echo “Position “ . $counter . “ holds value ” . $arr[$counter] . “<br />”;
}
?>
The first line of code makes an array with four elements (namely, the numbers 10, 20, 30 and 40). The while loop then displays each of these in turn, one to a line. To make the output more readable, we also add a bit of text (“Position “, “ holds value “, and a HTML line break code, “<br />”.)
Think again. Most businesses only have PART of a DR plan - and this spells business disaster in the event of an IT disaster.
Download The Seven Sins of Disaster Recovery White Paper now and find out how you can prevent this happening to you.