Home Accessibility Courses Twitter The Mouth Facebook Resources Site Map About Us Contact
 
For 2023 (and 2024 ...) - we are now fully retired from IT training.
We have made many, many friends over 25 years of teaching about Python, Tcl, Perl, PHP, Lua, Java, C and C++ - and MySQL, Linux and Solaris/SunOS too. Our training notes are now very much out of date, but due to upward compatability most of our examples remain operational and even relevant ad you are welcome to make us if them "as seen" and at your own risk.

Lisa and I (Graham) now live in what was our training centre in Melksham - happy to meet with former delegates here - but do check ahead before coming round. We are far from inactive - rather, enjoying the times that we are retired but still healthy enough in mind and body to be active!

I am also active in many other area and still look after a lot of web sites - you can find an index ((here))

Well House Consultants
You are on the site of Well House Consultants who provide Open Source Training Courses and business 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
Java Resources
Well House Manor Resources
Perl Resources
Python Resources
PHP Resources
Object Orientation and General topics
MySQL Resources
Linux / LAMP / Tomcat Resources
Well House Consultants Resources
Extras Resources
C and C++ Resources
Ruby Resources
Tcl/Tk Resources
Web and Intranet Resources
PHP module H105
Functions
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 subjectupdated
866A lazy programmer is a good programmer
I'm lazy. 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 ...
2011-06-27
 
1380Static 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 ...
2010-12-31
 
1021PHP - 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. function ...
2010-12-31
 
3026Coding 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 ...
2010-11-02
 
2929Passing 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 ...
2010-08-20
 
2737Improving 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 ...
2010-05-14
(longer)
2682Adding 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 ...
2010-03-25
 
2630Static 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 ...
2010-03-05
 
2488A variable number of arguments in a PHP function
Do you want to vary the number of arguments you pass in to a PHP function?    loadarticles(2,3,6,5,4);    loadarticles(5,7); 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 ...
2009-11-02
 
936Global, 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 ...
2009-01-01
 
339Passing 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 ...
2008-10-11
 
1784Global - 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 ...
2008-09-04
 
223There 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 ...
2008-05-16
 
96Variable Scope
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 ...
2008-05-11
 
1357Clean 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 ...
2007-09-18
 
1267is 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 ...
2007-07-16
 
1202Returning 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 ...
2007-05-24
 
1163A 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 ...
2007-04-26
 
775Do 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 ...
2006-06-30
 
308Call by name v call by value
All the programming languages that we train on provide the facility for you to write a named block of code and the call it from elsewhere - such blocks are known as subs in Perl, functions in PHP, Procs in Tcl, and functions or methods in Python. They're a vital part of any "grown up" programming system, ...
2006-06-05
 
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
Pictures
call by name and call by value - PHP
Background information
You may download this module as a sample of our material
Topics covered in this module
Global and static variables.
Loading functions from another file.
Defaulting parameters.
Call by value v call by name.
Example.
Object oriented PHP.
Complete learning
If you are looking for a complete course and not just a information on a single subject, visit our Listing and schedule page.

Well House Consultants specialise in training courses in Ruby, Lua, Python, Perl, PHP, and MySQL. We run Private Courses throughout the UK (and beyond for longer courses), and Public Courses 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.


You can Add a comment or ranking to this page

© WELL HOUSE CONSULTANTS LTD., 2024: 48 Spa Road • Melksham, Wiltshire • United Kingdom • SN12 7NY
PH: 01144 1225 708225 • EMAIL: info@wellho.net • WEB: http://www.wellho.net • SKYPE: wellho

PAGE: http://www.wellho.net/resources/H105.html • PAGE BUILT: Sun Oct 11 14:50:09 2020 • BUILD SYSTEM: JelliaJamb