Well House Consultants Samples Notes From Well House Consultants 1
Well House Consultants Samples Notes From Well House Consultants 1
Notes from
Well House
Consultants
These notes are written by Well House Consultants and distributed
under their Open Training Notes License. If a copy of this license is not
supplied at the end of these notes, please visit
http://www.wellho.net/net/whcotnl.html
for details.
Well House Consultants provides niche training, primarily but not exclusively in
Open Source programming languages. We offer public courses at our training centre
and private courses at your offices. We also make some of our training notes available
under our "Open Training Notes" license, such as we’re doing in this document here.
You are NOT allowed to charge (directly or indirectly) for the copying or distribu-
tion of these notes, nor are you allowed to charge for presentations making any use
of them.
If you would like us to attend a course (Java, Perl, Python, PHP, Tcl/Tk, MySQL
or Linux) presented by the author of these notes, please see our public course
schedule at
http://www.wellho.net/course/index.html
If you have a group of 4 or more trainees who require the same course at the same
time, it will cost you less to have us run a private course for you. Please visit our onsite
training page at
http://www.wellho.net/course/otc.html
which will give you details and costing information
Arrays
Arrays are PHP variables into which you can save more than one value.
You can then use a loop to process each individual value, you can refer to
individual values by using a special syntax, or you can perform operations
on the array as a whole.
Array manipulation . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 5
Array functionality . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 8
<head>
<title>Array lookup</title>
</head>
<body bgcolor=white>
Who lives where?<BR>
<?php
$house = $_GET[house];
$what = 16;
$home[1] = "John Smith";
$home[2] = "Daphne Jones";
$home[3] = "Ronald McDonald";
$home[10] = "The Prime Minister";
$home[11] = "The Chancellor";
$home[404] = "Lisa and Graham";
$home[Llareggub] = "Dylan Thomas";
$home[$what] = "Fred Jones";
If we run that (using the variable "$house" defined via the form), we'll get:
All successful lookups in the table. You'll notice that there's no distinction
between numbers and names for the houses, and that gaps are allowed in the
numbers.
1
to see if it exists, or if it contains a value
<head>
<title>Array lookup, with check etc</title>
</head>
<body bgcolor=white>
Who lives where?<BR>
<?php
$house = $_GET[house];
$home[1] = "John Smith";
$home[2] = "Daphne Jones";
$home[3] = "Ronald McDonald";
$home[10] = "The Prime Minister";
$home[11] = "The Chancellor";
$home[12] = "";
$home[404] = "Lisa and Graham";
$home["Llareggub"] = "Dylan Thomas";
if ($house) {
if ($home[$house]) {
print ("In $house lives $home[$house]");
} else {
print ("In $house lives we know not who");
}
} else {
print ("So you want a street listing then?<BR>");
reset($home);
while ($residence = each($home)) {
$addy = $residence["key"];
$person = $residence["value"];
print ("$person lives in $addy<BR>");
}
}
?>
</body>
You'll notice that we check to see whether we've got anything (non-empty) in
"$house" to see if our user completed the form, and then whether the particular
element is empty to see if there's anything to report. If a variable doesn't exist, it will
report back a false value, but we need to be careful if a house is empty ...
1
elements, indexes, keys - call them whatever you like!
1
"$home" in our previous example
2
an unusual one this, since it's written on the left of an assignment
3
we saw that earlier in this module
in computers in recent years, the authors of PHP have been able to add a huge raft of
functionality without any appreciable hit on performance.
We suggest that newcomers to the language learn a subset of the functions that
they're comfortable with, and then look up extra functions when they find themselves
thinking "there should be a function to do that" ... as there probably is. Don't try to
learn them all at once; you wouldn't try to learn every word in a foreign language as
your first step to learning it, now would you?
This extra flexibility can bring a headache for maintenance engineers, especially
those of them who only maintain PHP for a small proportion of their time. If you're
going to be writing code, consider who will be looking after it later. It's said that only
20 percent of commercial code is maintained by its author throughout its life, and
that much the heaviest expense of code is not the original writing but the upgrading
and maintenance during its life. Consider:
• Comment your code well
• Provide documentation too
• Inset your blocks so that they can be followed
• Structure your code into functions
• Use a sensible subset of the functions that PHP provides (not as many as you can!)
<head>
<title>Array lookup, with check etc</title>
</head>
<body bgcolor=white>
Who lives where?<BR>
<?php
function prwho($person,$addy) {
$person or $person = "no-one";
print ("$person lives in $addy<BR>");
}
if ($house = $_GET[house]) {
if ($home[$house]) {
print ("In $house lives $home[$house]");
} else {
if (is_string($home[$house])) {
print ("$house is empty");
} else {
print ("Don't know about
$house");
}
}
} else {
print ("So you want a street listing then?<BR>");
array_walk($home,"prwho");
}
?>
</body>
If you're already an experienced programmer1, you might like to note that you can
have arrays of arrays. Use two (or more) sets of square brackets after a variable name,
and it all works for you. Newcomers to programming are advised to wait a while
before they jump too deeply into this one!
Coming back to our recent comments about the flexibility of PHP, did you notice
the following in that last example?
if ($house = $_GET[house]) {
which assigns the value from the form into the house field and also checks it at the
same time, and:
1
or if you want to blow your mind now
License
These notes are distributed under the Well House Consultants
Open Training Notes License. Basically, if you distribute it and use it
for free, we’ll let you have it for free. If you charge for its distribution of
use, we’ll charge.
Please send any amendments and corrections to these notes to the Copyright
holder - under the spirit of the Open Distribution license, we will incorporate suit-
able changes into future releases for the use of the community.
If you are charged for this material, or for presentation of a course (Other than by
Well House Consultants) using this material, please let us know. It is a violation of
the license under which this notes are distributed for such a charge to be made,
except by the Copyright Holder.
If you would like Well House Consultants to use this material to present a training
course for your organisation, or if you wish to attend a public course is one is avail-
able, please contact us or see our web site - http://www.wellho.net - for further
details.
Change log
Original Version, Well House Consultants, 2004
License Ends.