Essentially when you invoke (call) a function you are giving an instruction to some code to do something for you.
The message that function returns to whoever used it (called it) is what you design into your function. That message which is passed back is called the return value.
There are times you might want a function to just do something and if it fails, to just shut up - but these are relatively rare things - so for now always return something.
So generally, you want your function to return one of these 3 things:
[B]true /B which usually equates to “that operation worked fine!”
[B]false /B which usually equates to “nope, something went wrong!”
OR
a value (anything) which is usually the result of what you have asked the function to just get on and do.
Your calling code can then inspect this return value to detect if the operation went well or not.
Here is a contrived example, lets imagine you have a website, or series of websites which deal with peoples’ names, and you are constantly adding first name to second name etc.
function makeProperName($name, $family=null){
if( $family === null ){
// today you decide that you cannot make a name without the family name
return false;
}
else {
return ucwords($name) . ' ' . ucwords($family) ;
}
}
/*untested by the way*/
So you have that function, and you keep it in an include file somewhere with some other similar functions you call nameFunctions.php
Now, deep squirreled away in your CMS, or website or forum or whatever, you are doing this.
include 'nameFunctions.php' ;
$name = "joe";
$family = "bloggs";
echo 'Welcome back ' . makeProperName($name, $family);
OR, more likely you are doing this:
include 'nameFunctions.php' ;
// $row = connect to database, get this persons row
echo 'Welcome back ' . makeProperName($row['name'], $row['family']);
In the above case you are using makeProperName($row[‘name’], $row[‘family’]) to be equivalent to a variable which contains the string “Joe Bloggs”.
But what if elsewhere, joe did not tell you his family name, and because family name is a mission critical detail which means you cannot go on (so you should have captured it earlier, right? Remember this is a contrived example, bear with me).
So now you can do :
include 'nameFunctions.php' ;
// $row = connect to database, get this persons row
$proper_name = makeProperName($row['name'], $row['family']);
if( $proper_name !== false){
echo 'Welcome back ' . makeProperName($row['name'], $row['family']);
}
else{
echo 'Hey, what is your family name?'
}
In the above case, if the row for joe has the value null in your database makeProperName($row[‘name’], $row[‘family’]) may well contain the value false or else a valid string.
Now your calling code is testing the return value because you are aware things could have gone wrong viz makeProperName() did not get all the information it needed, so you have then intelligently forked your code.
There are also other benefits to using functions which now start clocking up.
You notice that the variable $family is used inside the function, that means you are also free to now use that variable name again in another function, and you can use that $family variable in your calling code without worry of them getting mixed up and overwriting each other.
Now you might argue that you would be just as well doing :
echo 'Welcome back ' . ucwords( $row['name']) . ' ' . $ucwords($row['family']);
But consider this. Lets say that after a few weeks and having used your famous makeProperName() function in 10 different places you suddenly notice that some people start adding a middle name, and you want to incorporate that into your old makeProperName() function from now on but you don’t want to have to hunt and find those 10 old places.
So you can change the function signature to:
function makeProperName($name, $family=null, $middle=null){
// insert the middle name where it should go, as long as $family isset
}
and make the necessary addition to your function - so that it will still return false on failure.
And then oh, you get requests for a French version of your site and there they quite bizarrely write “BLOGGS Joe”… so you have the opportunity to tell makeProperName() do that for you - or this is getting too contrived now?
So functions provide a really cool way to package up discrete but repeated instructions and isolate the code that performs the message - and if you write them correctly you can inspect the return value (message) in order to decide what to do next.
The hardest trick when starting out is to identify likely function candidates which apply to your particular domain, market, niche call it what you will - because it seems all the really obvious ones are already written into php as php functions.
This is my best advice, and there are reasons to break just about all the points I made, but understand why - and keep your functions very very focussed - doing one simple thing and doing it properly.
As for how best to write them, there are always people here who will help you if you ask.
HTH