Writing function in PHP

Any problem with PHP can be disscused here
Post Reply
anish
Posts: 353
Joined: Fri Apr 27, 2007 12:34 pm
Contact:

Writing function in PHP

Post by anish »

Well, it has been a while since I offered a tutorial here at AstaHost. Most of my creativity has gone toward my new website, Handy PHP. The website is just getting started and it is hard to post potential content for my website here instead of there.

The purpose of this tutorial is to show you how to convert a standard PHP script into a reusable PHP function. It is funny, because this tutorial is very similar in nature to the very first tutorial I wrote here. Before we start, I'll try to explain the benefits of using a function instead of regular code for your scripts.

Functions consolidate several lines of code into a single entity that can be requested over and over again. Imagine that your function is an art box and that your art box contains everything you need to draw a picture. You know that any time you want to draw a picture, you simply need to use your art box! If you do not have an art box, every time you want to draw a picture, you have to go find each of the needed items before you can start to draw!


Imagine that you have the following script:
[php] <?php
// First our input string:
$a = "train, car, horse, mule, truck, plane, trolly, bus";

// Convert our input string into an array:
$b = split(',', $a);

// Next we need to remove any extra whitespace from our value to ensure proper sorting:
$i = 0;
foreach ($b as $c) {
$b[$i] = trim($c);
$i++;
}

// Now we sort our values in alphabetical order:
sort($b, SORT_STRING);

// We now convert our array back to a string:
$c = implode(", ", $b);

// Finally, we output the results:
echo "<pre>\n";
echo "Input: \"" . $a . "\"\n";
echo "Output: \"" . $c . "\"\n";
echo "</pre>\n";
?>
[/php]Would output:
Input: "train, car, horse, mule, truck, plane, trolly, bus"
Output: "bus, car, horse, mule, plane, train, trolly, truck"

While this script is not very long, it could become tiredsome to have to copy it every time we want to sort some values. Imagine that we have several strings of values we want to sort in various ways:
[php]<?php

// SORT THE VEHICLES WE COMMONLY USE ALPHABETICALLY!!!!!!!!!
// First our input string:
$a1 = "train, car, horse, mule, truck, plane, trolly, bus";

// Convert our input string into an array:
$b1 = split(',', $a1);

// Next we need to remove any extra whitespace from our value to ensure proper sorting:
$i1 = 0;
foreach ($b1 as $c1) {
$b1[$i1] = trim($c1);
$i1++;
}

// Now we sort our values in alphabetical order:
sort($b1, SORT_STRING);

// We now convert our array back to a string:
$c1 = implode(", ", $b1);

// Finally, we output the results:
echo "<pre>\n";
echo "Input: \"" . $a1 . "\"\n";
echo "Output: \"" . $c1 . "\"\n";
echo "</pre>\n";


// SORT OUR AGED NUMERICALLY IN REVERSE

// First our input string:
$a2 = "32,25,65,85,45,15,75,35,24,26,46,29";

// Convert our input string into an array:
$b2 = split(',', $a2);

// Next we need to remove any extra whitespace from our value to ensure proper sorting:
$i2 = 0;
foreach ($b2 as $c2) {
$b2[$i2] = trim($c2);
$i2++;
}

// Now we sort our values in alphabetical order:
rsort($b2, SORT_NUMERIC);

// We now convert our array back to a string:
$c2 = implode(",", $b2);

// Finally, we output the results:
echo "<pre>\n";
echo "Input: \"" . $a2 . "\"\n";
echo "Output: \"" . $c2 . "\"\n";
echo "</pre>\n";


// SORT OUR FRUITS ALPHBETICALLY IN REVERSE

// First our input string:
$a3 = "peach, pear, orange, apple, ****, kiwi, cherry, lemon, watermelon, lime";

// Convert our input string into an array:
$b3 = split(',', $a3);

// Next we need to remove any extra whitespace from our value to ensure proper sorting:
$i3 = 0;
foreach ($b3 as $c3) {
$b3[$i3] = trim($c3);
$i3++;
}

// Now we sort our values in alphabetical order:
rsort($b3, SORT_STRING);

// We now convert our array back to a string:
$c3 = implode(", ", $b3);

// Finally, we output the results:
echo "<pre>\n";
echo "Input: \"" . $a3 . "\"\n";
echo "Output: \"" . $c3 . "\"\n";
echo "</pre>\n";
?>
[/php]Would output:
Input: "train, car, horse, mule, truck, plane, trolly, bus"
Output: "bus, car, horse, mule, plane, train, trolly, truck"

Input: "32,25,65,85,45,15,75,35,24,26,46,29"
Output: "85,75,65,46,45,35,32,29,26,25,24,15"

Input: "peach, pear, orange, apple, ****, kiwi, cherry, lemon, watermelon, lime"
Output: "watermellon, pear, peach, orange, lime, lemon, kiwi, ****, cherry, apple"
As you can see, the code has gotten much larger and we are just doing the same thing over and over again.
What if we had 100 lists to sort? That would be a very large script wouldn't?

In order to create a function to replace all of this code, we first have to find the differences between each of the sections of code.
The very first difference is the $a variable ($a1, $a2, $a3).
- The input value is different in each case.
The next difference is the sort method: (sort($b1, SORT_STRING), rsort($b2, SORT_NUMERIC), rsort($b3, SORT_STRING)).
- Either sort normally or in reverse with sort or rsort.
- Either sort numerically or alphabetically with SORT_NUMERIC or SORT_STRING
And the final difference is the implode method :( implode(", ", $b1), implode(",", $b2), implode(", ", $b3)).
- Either add a comma between each item or a comma and a space between each item.

These differences will be our function arguments.
We will name our function list_sort().
Our function arguments would be as follows:
$input
$sort_order
$sort_type
$seperator
As a result, our function would start like so:

Code: Select all

function list_sort($input, $sort_order, $sort_type, $seperator)
Now to use the original code as the base for out function:
[php]<?php
function list_sort($input, $sort_order, $sort_type, $seperator){

// First our input string:
$a = $input;

// Convert our input string into an array:
$b = split(',', $a);

// Next we need to remove any extra whitespace from our value to ensure proper sorting:
$i = 0;
foreach ($b as $c) {
$b[$i] = trim($c);
$i++;
}

// Select the correct sort method using a switch:
switch($sort_order){
case 'normal':
sort($b, $sort_type);
break;
case 'reverse':
rsort($b, $sort_type);
break;
default:
sort($b, $sort_type);
break;
}

// We now convert our array back to a string:
$c = implode($seperator, $b);
return $c;
}
$a1 = "train, car, horse, mule, truck, plane, trolly, bus";
$a2 = "32,25,65,85,45,15,75,35,24,26,46,29";
$a3 = "peach, pear, orange, apple, ****, kiwi, cherry, lemon, watermelon, lime";

// Finally, we output the results:
echo "<pre>\n";
echo "Input: \"" . $a1 . "\"\n";
echo "Output: \"" . list_sort($a1, 'normal', SORT_STRING, ', ') . "\"\n";
echo "</pre>\n";

echo "<pre>\n";
echo "Input: \"" . $a2 . "\"\n";
echo "Output: \"" . list_sort($a2, 'reverse', SORT_NUMERIC, ',') . "\"\n";
echo "</pre>\n";

echo "<pre>\n";
echo "Input: \"" . $a3 . "\"\n";
echo "Output: \"" . list_sort($a3, 'reverse', SORT_STRING, ', ') . "\"\n";
echo "</pre>\n";
?>
[/php]
Would output:
Input: "train, car, horse, mule, truck, plane, trolly, bus"
Output: "bus, car, horse, mule, plane, train, trolly, truck"

Input: "32,25,65,85,45,15,75,35,24,26,46,29"
Output: "85,75,65,46,45,35,32,29,26,25,24,15"

Input: "peach, pear, orange, apple, ****, kiwi, cherry, lemon, watermelon, lime"
Output: "watermellon, pear, peach, orange, lime, lemon, kiwi, ****, cherry, apple"
As you can see, we haven't added very much code but have managed to replace the script with a function.

There a several areas of this example that provide for improvement opportunities.


reece
Posts: 55
Joined: Tue Apr 04, 2006 10:59 am

Post by reece »

thanks i like it, like the split function
4ran

Post by 4ran »

Thank very muh friend ! that was really useful :)
Post Reply