Functions

What is a function?

Functions are a way of reusing your PHP code without copy-and-paste. A common example is generating a password. This requires a few steps, and so it will be a few lines of code. We will want to generate passwords from more than one place on our site: for example on a ‘send me a new password’ page, as well as from a page where an admin can create user accounts. If we just copy and paste the code to all the places we need it that will work pretty well initially, but what about future changes? What if we are asked to change the site policy about what characters are valid in a password, or how long the password should be? We’ll have to go and find every copy of the password code and change all of them. If one is missed, we’ll have a mess.
That’s why we should make this a function. Now we can put the password generation in a single place, and call it anywhere. When it needs updating we update it in that one place and the changes are applied wherever we use it.

How to declare a function

1
2
3
function myFunction() {
   echo ‘hi’;
}

Line 1. The keyword function tells PHP we’re declaring a function. The function name follows that, and then parentheses, followed by an open curly bracket.
Line 2. The body of the function goes here. This one is pretty basic!
Line 3. Note the closing curly bracket.

How to call a function

To call a function, just use the name of the function in your code followed by brackets:
echo myFunction();

How to get some value back from a function

Say we want to do something other than just echo text to the browser. We can use PHP’s return feature:

1
2
3
4
5
function makeIceCreamOrder() {
$myArray = array(‘scoop_1’ => ‘chocolate’, ‘scoop_2’ => ‘strawberry’, ‘cone_type’ => ‘sugar cone’);
$iceCream = $myArray[‘scoop_1’] . ‘ and ‘ . $myArray[‘scoop_2’] . ‘ in a ‘ . $myArray[‘cone_type’];
return $iceCream;
}

To call this function and do something with it, we might write something like this:

1
2
$myOrder = makeIceCreamOrder();
echo ‘I would like ’ . $myOrder ., please’;

We are echoing the returned value here, but we could just as easily use it as input to another function here.

How to pass a variable to a function

Say we want to make our ice cream function more useful?

1
2
3
4
function makeIceCreamOrder($scoop_1, $scoop2, $cone_type) {
$iceCream = $scoop_1 . ‘ and ‘ . $scoop_2 . ‘ in a ‘ . $cone_type;
return $iceCream;
}

Here’s how I might want to call the function above:

1
2
$myOrder = makeIceCreamOrder(‘cookies and cream’, ‘mint’, ‘waffle cone’);
echo ‘I would like ’ . $myOrder ., please’;

You can also pass arrays to functions. We’ll look at this in our project.

This post is part of PHP First Steps Tutorial. Go on to the next session: Debugging.

24 hr tutorial Hey!
Based on the feedback from readers of this tutorial series I’ve expanded, revised, and improved this brief intro as a whole book. It is much longer, so you get a more in-depth guide to getting started. As a result you’ll write better code. When you buy the book you also get a download password for all the source code from the tutorials. Build a functioning blog from scratch, and other useful mini-projects! Check it out »

No Comments

Be the first to start the conversation.

Leave a Reply

*

Text formatting is available via select HTML.

<a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>