You have already used many functions with PHP. Every time you do:
<?php
explode(' ', $value1);
?>
you're using a function. These are known as built in PHP functions because they are the same and are available on every PHP setup. You can access them in any script without needing to do anything special.
A function can be classed as a subprogram that can act on data and return a value. Each function has its own name and when that name is encountered, the execution of the programme branches off to the first line (or statement) of that function. When the function has completed, it 'returns' and execution resumes from where it left off. A function will perform a specific task and do it well, returning the value you want with the arguments you pass to it.
When you lookup a function on the PHP website e.g
explode(), you will see the description of the function, and what it does first. The description then explains how to use the function.
In the case of explode:
array explode ( string separator, string string [, int limit]) This line is known as the prototype. It tells you exactly what the function is and what parameters it should be passed. We can see that it will return an array when you call it, and as parameters (the name of a variable usually passed to a function e.g
$split and
$string are the parameters in
explode($split, $string)), it takes separator, string and limit. Parameters are always within the
( ). The first 2:
( string separator, string string [, int limit]) are required and you must pass arguments (the actual value of the parameter e.g
' ' and
'split this up' are the arguments in
explode(' ', 'split this up')) for these when you call the function. If you do not specify a separator or a string, you will get an error. The parameters must also be passed in the order they are listed here. You cannot swap them around.
However, there is the limit parameter at the end:
( string separator, string string [, int limit] ) Notice how this is within [squared brackets]. This means it is optional. You do not need to pass that parameter to the function.
The return and parameter types (highlighted below) are not really important in most cases.
array explode ( string separator, string string [, int limit]) PHP is known as a loosely typed language which means you do not need to worry about variable types. PHP will do the work for you. In the case of
explode(), you know however that it will return an array when it is completed and you have to pass it 2 strings. You would get an error if you tried to pass it an integer:
explode(1, 15117);
But generally speaking, you can safely ignore the types.
When calling the function, you can do several things with the result. For example, you could echo it directly to the page:
echo explode(' ' , 'split this up');
Which wouldn't be much use. The most common thing you'd do is save the result in a variable like so:
$split = explode(' ' , 'split this up');
You can then play with the data as you like because it is in the $split variable.