IMPORTANT NOTE: If you copy code out into your editor, it copies the wrong type of quotation marks “. Please use standard ones or your scripts wont work. (shift 2 gives you normal ones)
Functions, are a handy tool in any language, and PHP is no different, so today we’re going to get your php functions sorted. We’re going to run through some examples, and I cannot garantee they won’t be related to my favourite ‘spoon’ examples. Ok lets start, with the benefits of functions.
Functions provide reusable code for repetitive tasks. Functions can improve your code by using less lines of code to do the same tasks. Instead of re-writing an action over and over, you can write it once with a function and re-use it as much as you like. You will probably already use functions in your code which are built into PHP without realising it (if you are new to PHP). Today we’re focusing on writting your own functions, and the rest will fall into place.
<?php // start php // name the php function function spoonOutput() { // tell the function what you want it to do echo "I am a spoon"; } // rest of you code here and how to use the function spoonOutput();
?>
Ok its going to be a faily good idea to let you know whats going on here, so lests do so. We start by starting our php code in the usual manor. Then we define our function, and give it a name, we call it spoonOutput, please remember that function names are case senstive. Please don’t go naming them stupid things either. The brakets ‘()’ are where in more complicated functions you could use variables, but we will cover this later. Ok next, we put what we want our function to do, thats with in the curly brackets { & }. Which in this case we’ve told it to echo ‘I am a spoon’. Then finally, we use our function simply by putting ‘spoonOutput();’ Every single time you put that, it will execute the function and output ‘I am a spoon’.
We can reuse this function as much as we like now, which is great, and the whole point of functions. So there we go, officially, and technically, you can say you can write functions, though most people will just think it’s crap.
Ok another example. PHP functions with paramaters, lets use those ( ) bad boys… Oh god, can’t believe I just said that, someone kill me.
<?php // start php going
// give your function a name
function spoonSize($size) {
echo $size . "spoon.<br />";
}echo "I have a ";
// use your function to finish the sentence
spoonSize("Tiny");
echo "I have a ";
// use your function to finish the sentence
spoonSize("Massive");
?>
Ok, just so you know the output of all that would be :
I have a Tiny spoon
I have a Massive spoon
Obviously you can re-write and reuse this function as much as you like, but by now you should start to realise how powerful it is.
OK I’m just going to do 1 more popular function example, just adding 2 things together, it’s been done a hundred times before, but whats one more time?
<?php // start php going // name the function function addShit($a,$b) { $total = $a + $b; return $total; } // run the function echo "8008 + 7355 = " . addShit(8008,7355);
?>
This would output:
8008 + 7355 = 15363
Ok just to explain what is going on there, its not hard to work out after the last examples I’ve done, but basically we start php, then we’ve named our function, told it to add $a & $b together then we’ve given it a sum to work out, run the function.
Hope you found this useful.
Thanks
Wuup Team
IMPORTANT NOTE: If you copy code out into your editor, it copies the wrong type of quotation marks “. Please use standard ones or your scripts wont work. (shift 2 gives you normal ones)




Recent Comments