Home Accessibility Courses Twitter The Mouth Facebook Resources Site Map About Us Contact
Python and Tcl - public course schedule [here]
Private courses on your site - see [here]
Please ask about maintenance training for Perl, PHP, Lua, etc
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 want to be able to pull in code from included files written by others without the need to avoid any variable names that the others may have used, but it does mean that the language has to provide mechanisms to get information into and back from functions so that they don't run in complete isolation from one another.

Here's a review of the ways of sharing data between functions, or saving data from one use of a function to the next.

Parameters. Parameters are passed in to functions in a list in round brackets after the call, and the names that will be used for the parameters are declared in a similar length list within the function definition. Note that by default the values passed to a function are COPIED in to those variables, so that any changes made are NOT reflected in the source variable in the main code.

Return Values. At the end of a function, you can choose to return a variable or an expression, and that will be saved into a variable in the main code is you choose to assign it, or you can test it in a conditional statement or include it in another expression if you wish. Although only one variable can be passed back, it can be an array - so that in effect you can pass back a collection of values.

Parameters and return values are the recommended way of passing most information in and out of functions. The other facilities listed below all have their uses, but they should not be used to excess.

Default parameters. If you declare a function with a defaulted parameter (e.g. if you say $abc=17.5 rather than just $abc in the function definition), you're then allowed to call the function omitting the parameter and if you do it will assume the value given. Very useful if you have a function that has parameter(s) that are only needed on some occasions.

Parameters by name. If you declare a function with a leading & character in front of the $ - so &$abc rather than $abc for example - then the parameter will not be copied in to the function, but rather the internal variable name will be used as another name for the same piece of data in the calling code. This means that if you alter the variable in the function, you're also altering the variable with which it is connected in the main code. A very useful facility, but very dangerous if the calling programmer isn't aware that your function may change his settings.

Globals. If you declare a variable to be global in a function, then that variable is the same as the variable of the same name in your main code. There's no need to declare anything as global in the main code - things always ARE global there. Use this facility sparingly, as it can cause functions to be less portable now that they work with fixed name values, and it can bring variable name conflicts one step closer. If you want to share variables between several functions, you can declare them to be global in each function and it will work - just be careful you don't use the same variable name for something else in the main code!

Super-Globals. PHP provides you with a number of variables such as $_SESSION and $_REQUEST which as known as "superglobals". They're always available in any functions without the need to declare them, and a reference to a superglobal within a function is the same as a reference to a superglobal in the main code.

Statics. Usually, a function's local variables are lost when you exit from a function and then they'll be re-initialised next time the function is called. That's usually the behaviour that you want, since most of the time you're likely to be calling functions several times with each of the calls being unrelated to previous calls. However, if you want a variable to persist from once call to the nest, you can declare it as static.
(written 2005-06-07, updated 2008-10-11)

Commentatorsays ...
Graham:As an addendum ... I was asked yesterday if you can define your own superglobals. I gave the poliicians answer - "why on earth would you want to?" - and didn't address the questions directly. A good answer since I didn't want to encourage the class to even try, but in reallity the answer is "not exactly". Superglobals are reserved PREDEFINED variables ... but there's nothing to stop you adding an element into the superglobal array $GLOBALS (or any of the others if you really want)
(comment added 2005-06-09 07:57:15)
Associated topics are indexed as below, or enter http://melksh.am/nnnn for individual articles
H105 - PHP - Functions
  [3026] Coding efficiency - do not repeat yourself! - (2010-11-02)
  [2929] Passing a variable number of parameters in to a function / method - (2010-08-20)
  [2737] Improving your function calls (APIs) - General and PHP - (2010-04-24)
  [2682] Adding extensions to PHP Open Source applications - callbacks - (2010-03-17)
  [2630] Static variables and ampersands in PHP - (2010-02-10)
  [2488] A variable number of arguments in a PHP function - (2009-11-02)
  [1784] Global - Tcl, PHP, Python - (2008-09-03)
  [1380] Static variables in PHP - (2007-10-05)
  [1357] Clean my plate, but keep my wine bottle. (PHP; Static) - (2007-09-18)
  [1267] is there a lookup function in php? - (2007-07-15)
  [1202] Returning multiple values from a function (Perl, PHP, Python) - (2007-05-24)
  [1163] A better alternative to cutting and pasting code - (2007-04-26)
  [1021] PHP - static declaration - (2007-01-04)
  [936] Global, Superglobal, Session variables - scope and persistance in PHP - (2006-11-21)
  [866] A lazy programmer is a good programmer - (2006-09-15)
  [775] Do not duplicate your code - (2006-06-23)
  [421] Don't repeat code - use loops or functions - (2005-08-21)
  [409] Functions and commands with dangerous names - (2005-08-11)
  [340] Code and code maintainance efficiency - (2005-06-08)
  [308] Call by name v call by value - (2005-05-11)
  [223] There is a function in PHP to do that - (2005-02-21)
  [96] Variable Scope - (2004-10-22)

Back to
OO techniques are hard to teach
Previous and next
Horse's mouth home
Forward to
Code and code maintainance efficiency
Some other Articles
Should I use structured or object oriented?
The evening after the course
Happy Birthday, PHP
Passing information into and out of PHP functions
OO techniques are hard to teach
the array returned by preg_match_all
Targetted Advertising
Sad priorities
Symbolic links and hard links
4759 posts, page by page
Link to page ... 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51, 52, 53, 54, 55, 56, 57, 58, 59, 60, 61, 62, 63, 64, 65, 66, 67, 68, 69, 70, 71, 72, 73, 74, 75, 76, 77, 78, 79, 80, 81, 82, 83, 84, 85, 86, 87, 88, 89, 90, 91, 92, 93, 94, 95, 96 at 50 posts per page

This is a page archived from The Horse's Mouth at http://www.wellho.net/horse/ - the diary and writings of Graham Ellis. Every attempt was made to provide current information at the time the page was written, but things do move forward in our business - new software releases, price changes, new techniques. Please check back via our main site for current courses, prices, versions, etc - any mention of a price in "The Horse's Mouth" cannot be taken as an offer to supply at that price.

Link to Ezine home page (for reading).
Link to Blogging home page (to add comments).

You can Add a comment or ranking to this page

© WELL HOUSE CONSULTANTS LTD., 2019: 404 The Spa • Melksham, Wiltshire • United Kingdom • SN12 6QL
PH: 01225 708225 • EMAIL: info@wellho.net • WEB: http://www.wellho.net • SKYPE: wellho

PAGE: http://www.wellho.net/mouth/339_Pass ... tions.html • PAGE BUILT: Sat May 27 16:49:10 2017 • BUILD SYSTEM: WomanWithCat