Like most other programming languages, PHP offers the ability to create functions (sometimes called methods). Functions in general are small prewritten operations that can manipulate/create data or perform certain tasks. They can range from fairly large complex instructions to small and simpler instructions. Their main purpose is to ease the programming burden by creating small modules of code that we can call upon over and over again if we need to perform their operation more than once.
PHP functions follow a format for their creation in likeness to those of other languages.
There are certain rules to creating a function and these rules, if not followed will result in a compiler error. So basically, they have to be followed.
Here is an example of a small function that removes all characters from a String that are not numeric:
function removeAlpha($string)
{
$stringChars = preg_split('//', $string, -1);
$numericStrin = "";
for($c = 0; $c < count($stringChars); $c++)
{
if( is_numeric($stringChars[$c]) )
{
$numericString .= $stringChars[$c];
}
}
return $numericString;
}
// written by Jordan Savant
Let’s take the function apart one by one and explain the purpose for the syntax presented.
Line 1: function removeAlpha($string)
This is the declaration of the function, and also called the function’s signature. When you are defining a function you must present the word “function” before writing the function’s name and parameters. Inside of the parenthesis, you can place all of the variables you wish for the function to accept. In this example we are excepting a single variable called “$string” which contains our string we want to operate on. If you were to use more than one parameter, you would separate them with commas.
Line 3: $stringChars = preg_split(’//’, $string, -1);
In this line we are calling upon a PHP ready function called preg_split(). preg_split() is a function that can split a string into tokens or parts based upon patterns in the string. For instance you could break the string into pieces every comma, period, colon, space, or any character. In this particular example we are breaking it into tokens with no identifying split marker. In other words we are splitting the string at every single character in it. This function returns an array of those characters in the string which we store in the variable $stringChars.
Line 4: $numericStrin = “”;
In this line we are simply initializing a variable called $numericString. Our function will use this variable internally as you will see shortly.
Line 5: for($c = 0; $c < count($stringChars); $c++)
Here we are creating a loop that will cycle the same amount of times as the length of the array. This means that we will have as many cycles as we will characters in our $stringChars array. This will allow us to work on each individual character in the array one by one.
Line 7: if( is_numeric($stringChars[$c]) )
Here we are creating an if statement. Inside of the condition of this if statement we are using a PHP prebuilt function called is_numeric(). This is_numeric() function checks to see if the variable it is given contains only numbers. If it is only filled with numbers it will return a true, otherwise it will return a false. Since we are asking whether or not the current character is a number we want to act upon it as so. If it returns true then we will run the if statement, otherwise we will skip the operation in the if statement.
Line 9: $numericString .= $stringChars[$c];
The instructions given on this line of code will only be executed if our previous if statement condition is true. In other words, if our character is actually a number we execute this line. More particularly, this line actually takes the current value in the array and concatenates it with the variable $numericString (concatenate means to add to the end or append). So for each character that is a number, we add it to $numericString. By the time our for loop ends the $numericString variable will contain a string of the numeric values in our original string only.
Line 12: return $numericString;
This is the final instruction in our function. When the loop has ended and our numbers in the string have been found, we will want to be able to use that numeric string. Thus the “return” command is required. In a function code outside is not allowed to access the data inside. Therefore, if we want to get the data back from the function we are required to return that data using the return command. Sometimes functions just perform tasks and do not return data. This is fine, and by simply putting “return;” as the instruction will work. On another note, we can accept as many parameters as we would want, but we can only return a single value from a function. So our function needs to be simple enough in nature that we desire only one result from it’s operation. And like we said before, our outside code cannot access the data inside of the function, for instance, the $stringChars array.
Here is an example of the usage of our removeAlpha() function:
$str = "this is a string with a 7, a 3, and a 29";
$newStr = removeAlpha($str);
echo $str;
echo $newStr;
$str contains : “this is 1 string with a 7, a 3, and a 29″.
$newStr contains : “17329″.
We can define our function either before or after the we call it in our code, but most importantly we must define it.
General Rules:
1. You must define a function using the “function” command.
2. You can pass as many parameters as needed to the function, but you can only return one.
3. Functions can call other functions, or even themselves.
4. Data created in the function is not accessible by outside code.
5. You must use a return statement when ending a function. It is an accepted convention for most languages.
6. Be creative, but not wasteful. (~:
Have fun writing functions and feel free to post any you may have created!