A Quick Guide to PHP Functions.
What is a function?
A function is a way of repeating PHP code over and over again quickly and easily. Instead of having 20 lines of code repeated 5 times, you use the 20 lines of code once, and repeat 1 line of code 5 times.
Ok. Now you understand exactly why functions are so useful, I’ll go into a bit more detail about how they can be used.
To create a function, type:
And all the code for the function goes in between those curly braces.
You can have a simple function like this:
And then you can call it:
And you will get a random number between 1-5. Now, in-between those brackets you can have parameters. This may confuse you at first, but once you get it it is very easy.
Just take a minute to read it over. Remember in the last function, the random function was between 1 and 5? Now, it will be between $min and $max. How do we assign $min and $max? When we call for the function:
It will give you a random number between 5 and 10. All I have done when calling the function is replaced $min with 5, and $max with 10.
If you want to assign the function to a variable, you can easily, just like you would for most variables.
Let’s make things a bit more complex. Readers of my ‘Beginning PHP’ will be familiar with this example. I want to work out the area of my rectangle, and the perimeter. Now, knowing what we do about parameters within functions, you should understand this code:
Slightly confused? I’m going to show the same function again, but replace $width and $height with a number, which should help you out.
Hopefully that is clear to you know. To call on the function, and assign the width and height values, we do:
That concludes a brief tour of functions. Hopefully you now understand how to create a function, how to call a function, and how functions spare PHP developers a lot of time.
Remember – It’s not about reading this, to learn anything you must type the code out for yourself.
Jack
What is a function?
A function is a way of repeating PHP code over and over again quickly and easily. Instead of having 20 lines of code repeated 5 times, you use the 20 lines of code once, and repeat 1 line of code 5 times.
Ok. Now you understand exactly why functions are so useful, I’ll go into a bit more detail about how they can be used.
To create a function, type:
PHP:
function name {
}
You can have a simple function like this:
PHP:
function random() {
return rand (1, 5);
}
PHP:
random();
PHP:
function random($min, $max) {
return rand ($min, $max);
}
PHP:
random(5, 10);
If you want to assign the function to a variable, you can easily, just like you would for most variables.
PHP:
$variable = random(5, 10);
PHP:
function rectangle($width, $height) {
$area = $width * $height;
$perim = ($width + $height) * 2;
echo ‘<h4>Area is: ‘.$area.’</h4>’;
echo ‘<h4>Perimeter is: ‘.$perim.’</h4>’;
}
PHP:
function rectangle(3, 5) {
$area = 3 * 5;
$perim = (3 + 5) * 2;
echo ‘<h4>Area is: 15 </h4>’;
echo ‘<h4>Perimeter is: 16</h4>’;
}
PHP:
rectangle (5, 10);
Remember – It’s not about reading this, to learn anything you must type the code out for yourself.
Jack