PHP Basics: Advanced function
Not the trickiest example function, however this will teach you how to output a value created from within the function.
PHP Functions (Arguments & Values)
Creating a basic fuction
Functions are great for cutting down on code and making things easier to read when developing, you can create a function
for just about any process including conditional statements, loops or even calls to other functions.
This article will show you how to create simple functions for your applications and website's.
First things first we need to know how to define and invoke a function:
<?php
// Define your function
function displayEinsteinQuote()
{
echo "Do not worry about your difficulties in Mathematics. I can assure you mine are still greater.";
}
?>
<?php // Invoke your Albert Einstein Quote displayEinsteinQuote() ?>
Once you invoke your function this will output:
Do not worry about your difficulties in Mathematics. I can assure you mine are still greater.
Now I know what you are thinking, why not just echo the quote to begin with? well the next example will show you how to use
a variable to create a more dynamic function
<?php
// Define your function with simple argument
function ConvertStoneToKilos($stone)
{
$diff = '6.35029318';
$total = ($stone*$diff);
echo "$stone stone is the equivilant to $total kilograms";
}
?>
<?php // Invoke the function ConvertStoneToKilos(11); ?>
This will convert eleven stone into kilograms (69.85322498), of coarse changing the varaiable in the brackets will change the output!

