pTutorial

New way to learn PHP

PHP 5 Function Libraries

What is Function Libraries?

Function libraries are one of the most efficient ways to use the concept of code reusability. For example, you may have written a function for sorting of an Array. You can probably use these functions repeatedly in various applications without written and copy paste this code again and again. Let's see the example

Array sorting

<?php

//save libraries.inc file
function A_order($number)
{
$a =  rort($number)
Return $a; 
}

function D_order($number)
{
$a =  rsort($number)
return $a; 
}

?>

The function library saves with .inc extension. Now your library are ready to use if you want to use this library simply include this library by using include () or require () statements. After including this library, all the functions of this library become available to use.

Syntax of include library

include(path/filename);
require(path/filename);

You can use one of the above PHP function to include the PHP function library.

Example of function libraries

<?php

include("libraries.inc");

echo "Descending order";

echo "Using function libraries ";

$num = Array(10,50,5,250,2);

$p = D_order($num);

?>

You can easily use the library function by including the.INC file in your program, as shown above.

Note: You can keep any number of functions in your library.

PHP function libraries example

<?php

include("libraries.inc");

echo "Ascending  order";

echo "Using function libraries
"; $num = Array(10,50,5,250,2); $p = A_order($num); ?>

QUOTES OF THE DAY

Tomorrow life is too late, live today.

LIKE ON FACEBOOK

+1 ON GOOGLE

SHARE ON TWITTER