Well House Consultants
You are on the site of
Well House Consultants
Open Source Training Courses
hotel accommodation. You are welcome to browse and use
our resources subject to our copyright statement and to add in links from your pages to ours.
Other subject areas - resources
Well House Manor Resources
Object Orientation and General topics
Linux / LAMP / Tomcat Resources
Well House Consultants Resources
C and C++ Resources
Web and Intranet Resources
PHP module H105
Exercises, examples and other material relating to training module H105. This topic is presented on public courses Learning to program in PHP
, PHP Programming
, Object Oriented Programming in PHP
, Beginning PHP - weekend course / hobby / club / leisure users
, PHP Programming
, Learning to program in PHP
If you have a piece of code that you want to share between a number of PHP web pages, write it into a function! A function is a named block of code into which you can pass a number of variables (known as parameters) and from which a result can be returned.
|Articles and tips on this subject||updated|
|866||A lazy programmer is a good programmer|
By which I mean, if a job can be done once in such a way that I don't have to keep doing it time and time again, then that's how I will try and do it, even if there's a bit more effort and cost at first. That's why (for example) we've laid an underground water pipe out to where we're planning ...
|1380||Static variables in PHP|
When you call a function, you typically want a nice clean start with no "leftovers" from the previous call. And that's what you get by default in PHP. However, there can be exceptions where you want data to be retained and you can do that using:
EITHER a global variable which is shared with the main ...
|1021||PHP - static declaration|
If you want to retain the value of a variable between one call to a function and the next, you can declare it as static. And as part of the initial declaration, you can assign a starting value to it. This allows you to test whether you're calling a function for the first time, or for a subsequent time.
|3026||Coding efficiency - do not repeat yourself!|
"If you find yourself repeating something ... there's probably a better way"
"If you find yourself thinking "surely someone's done this before" ... they probably have, and their code is probably available to you"
The things I find myself saying very frequently in courses ... for delegates who are new ...
|2929||Passing a variable number of parameters in to a function / method|
How many columns are there in each row in a table? It will vary, depending on the table!
If I want to write a "row" function in PHP, passing in as parameters each of my columns, I don't know the parameter count. But I can find out using the fun_num_args function, and I can then get each of them using ...
|2737||Improving your function calls (APIs) - General and PHP|
Some of the code at the Civil Service Department (CSD) where I worked on manpower planning models some 35 years ago would not have stood up to analysis against modern programming practice (but then ... that WAS 35 years ago). Indeed, some of the code "behind the curtains" didn't even stack up properly ...
|2682||Adding extensions to PHP Open Source applications - callbacks|
Over the last two days, I've installed and configured MediaWiki, WordPress, Status.net and Drupal on one of our training systems, and looked (in each case) at how to configure them and install extra plugins / modules - both standardly available ones, and also how to start writing your own.
In two ...
|2630||Static variables and ampersands in PHP|
If you call a function within your program, the last thing you usually want is to have the debris from a previous call left over and effecting how it runs. The fact that you previously looked for a florist in Melksham should not effect your search for a brewer in Devizes, and asking for the average ...
|2488||A variable number of arguments in a PHP function|
Do you want to vary the number of arguments you pass in to a PHP function?
Simply declare your function with a minimum of parameters that you need (perhaps zero) and use the func_get_args() function to get you an array of ...
|936||Global, Superglobal, Session variables - scope and persistance in PHP|
"Global" is a poor choice of word! It really means "share this variable in this function with the variable of the same name at the top level" but there isn't a simple, easy better word for that than "Global". If you bring in code within an include / require file, then any top level code's variables ...
|339||Passing information into and out of PHP functions|
In PHP, named blocks of code are referred to as "functions". By default, variables within functions are created each time the function is run, and do not conflict with any other similarly named functions elsewhere in your code. That's good news for coders of medium sized and larger applications, who ...
|1784||Global - Tcl, PHP, Python|
PHP, Python, Tcl and a number of other languages have a global keyword. And it's a misnomer in most (if not all) cases.
To the un-initiated, "Global" means "worldwide" or "shared all around". So you would think that if you declare something as global, you're going to be sharing it with the same thing ...
|223||There is a function in PHP to do that|
Once you understand the philosophy behind a programming language, you can make superb use of it. Initial learning will show you a few things and get you comfortable, but once you've also learnt a few of the things behind the language you can become a real wizard. Your educated guesses will usually be ...
One of the vital topics on all our programming courses is that of variable scope. Variable Scope may be defined as the area of a program in which a variable is visible, and how long that variable is accessible for.
Why do I describe variable scope as a vital subject when you can write simple programs ...
|1357||Clean my plate, but keep my wine bottle. (PHP; Static)|
On Monday morning, I served Colin's breakfast on a clean plate in the "Whitworth" ... one happy camper, judging by the crumbs and butter wrappers left on the plate, and the smile on Colin's face.
Never wanting to disturb a customer's property and space, I carefully preserved the plate as Colin left ...
|1267||is there a lookup function in php?|
Search engines often bring visitors to our web site ... usually to a relevant page for them, but sometimes to the wrong page on the right site. Here's an edited copy of an answer I've just sent to one such arrival, who was so upset at getting the wrong page that he wrote to me twice - two different ...
|1202||Returning multiple values from a function (Perl, PHP, Python)|
Function in PHP and Python and subs in Perl can only return one "thing" - one item, one object. But all three languages allow that returned item to be a collection - in other words, a composite. And all three languages provide a very easy way of breaking the returned structure down into a series of individual ...
|1163||A better alternative to cutting and pasting code|
If you're new to coding, you'll be so concerned to be writing code that works that you may not take a look at coding technique. Your nose will be so close to the grindstone as you work that you won't take the time to look and ask "Do I need to keep grinding anyway?"
If you find yourself writing a piece ...
|775||Do not duplicate your code|
If you've writing or maintaining a program and you find yourself cutting and pasting a chunk of code, STOP and think again.
By duplicating a block of code, you're duplicating your maintainance task from that point onwards - any fixes applied to the original much be applied to the copy too. And that's ...
|421||Don't repeat code - use loops or functions|
I've just answered a question about completing a whole series of radio boxes in a form, where the questioner provided sample code that had the same line, slightly modified, appearing time and time again. But if you find yourself repeating something, there must be an easier way and that way will be either ...
Examples from our training material
|bmi1.php|| control example - showing need for functions and includes|
|bmi2.php|| functions and includes to make for shorter, more maintainiable code|
|callback|| Calling a list of functions named in an array|
|dressing.inc|| example of "call by name" and defaulting parameters|
|dropahead.php|| Selection from the week ahead|
|footer.inc|| Inlcude file - a standard block of HTML|
|funk2.php|| Example with local, global and static variables|
|funk3.php|| Loading functions from an include file|
|funky.php|| Defining and calling a function|
|funsan.php|| Simple sanitisation and sticky field example using functions|
|hotdiary.php|| PHP Diary generator|
|medical.inc|| for scope3 demo|
|near.php|| Ordnance Survey and Map reference point calculations|
|personetc.inc|| for scope4 demo|
|scope1.php|| Keyboard to Screen - simple PHP program|
|scope2.php|| Use of a function to separate out complicated / repeated code|
|scope3.php|| function code now in separate file|
|scope4.php|| Now using an Object Oriented approach|
|stademo.php|| use of a static variable in PHP|
Picturescall by name and call by value - PHP
You may download this module
as a sample of our material
Topics covered in this module
Declaring and calling a function.
Arguments and return values.
Variable scope, global and static.
Loading functions from another file.
Library functions - overview.
Call by name and call by value.
If you are looking for a complete course and not just a information on a single subject, visit our Listing and schedule
Well House Consultants specialise in training courses in
. We run
throughout the UK (and beyond for longer courses), and
at our training centre in Melksham, Wiltshire, England.
It's surprisingly cost effective to come on our public courses -
even if you live in a different
country or continent to us
We have a technical library of over 700 books on the subjects on which we teach.
These books are available for reference at our training centre.