In this chapter we’ll cover functions with PHP. How to use them, where to look them up and how you can define your own.
FunctionsFunctions are what you use to make PHP manipulate and process data, or just do a big bunch of statements quickly and easily without having to type the same lines of code over and over. PHP has hundreds of available functions for you to use and there’s normally always one you can use for what you want to do. On the rare occasion there isn’t, you can just write your own.
Firstly, let’s look at how we use existing functions.
Look at the example, we used the strip_tags() function. What this function does is strip any HTML or PHP tags out of a string. Look:
The tags are gone. The function stripped them out and then returned a new version of the string, with no tags, back into the variable that we specified (
So the function strips the tags off the value that we pass to it, then we assign the new value from the function to
PHP has many built-in functions like the one above. You can search on PHP.net’s function list to find them (see form at top of page). It’s really down to memory, the more you get into PHP the more functions you’ll use and remember.
_“I see and I forget, I hear and I remember, I do and I understand.”—Confucious_
This applies to programming, too. Sit here reading and you’ll forget it, open up a text editor, write a program, test it, debug it and change it, play around with PHP and you will remember.
Parameters/ArgumentsFirstly parameter and argument is the same thing. Just some people call them parameters, some call them arguments. In the above example we passed one parameter to the function strip_tags. Functions aren’t limited to accepting one parameter, they can accept a number of parameters. Some functions require more than one parameter, some only have one, some require a certain amount of parameters or one parameter and have extra, optional parameters. strip_tags has an optional parameter that you can take advantage of. Let’s take a look.
Look, we passed an extra parameter to the function. The second parameter is a string (hence why it is surrounded by quotes) containing any tags we don’t want stripped from the string. We passed the function only one tag, the paragraph tag, so any paragraph tags in that string will not be stripped, but all other tags will. So the string returned from the function would be the string we submitted, minus any HTML or PHP tags, except for paragraph tags.
Okay now we know about parameters and how we call and use functions, let’s look at how we can write some of our own.
Custom FunctionsPHP allows us to define our own functions, let’s look at a very simple one, just to see the format.
Easy eh? We just write “function”, followed by the function name, followed by two brackets. We then open a curly bracket to start the function statements. It is ended by the closing of the curly brackets. We could now use this function in our script.
Doing that would call our function and run the statements inside it. That’s a pointless function, but it shows you how to define one. We could pass a parameter.
With that function we passed a parameter. So now we could do:
Passing the string
Returning a ValueTo make our function return a value is simple, this is how we do it.
We just do whatever we want to do, and then return
Note: in this function we used two new functions: substr() and strtoupper(). Click on the links to go to the PHP reference pages and see what these functions do. This is how you’ll learn functions if you’re going to get into PHP, so go read it.
So now we could do:
Our function returned the string which we then assigned to a variable. We could then pass this variable to other functions if we wanted, do whatever we want with it before we print it out. Have a play around with functions and get used to them. Before we move on, one more thing….
Getting GLOBALSometimes in functions you have to use global. Let’s look at some examples to show you why.
See how we use $thestring within the function? Well if we then, outside the function, typed
There’s two main reasons why we’d need this:
Here’s how we make a variable global.
What we did above was make a variable named $length which we wanted to be usable within the function too. The line that does it is
That’s about it on functions, in the next chapter we will look at an important part of your learning: Control Structures.
No Comments for this page.