Built in User Defined Functions in PHP

PHP & MySQL Course:Built-in User Defined Functions in PHP

In the previous lecture 5, we learnt something important about the comments, loops and arrays which helps us too

much during the website development. When we are using PHP & MySQL.

Lecture 6:Built-in  & User Defined Functions in PHP

In this lecture we will learn advanced PHP programming. In this lecture you will able to understand the uses of

functions in PHP and also some built-in functions. As we know that there are two types of functions Built-in functions

and user-defined functions.

  1. Built-in Functions in PHP
  2. User defined Functions in PHP
  3. How to sort arrays in PHP

Built-in Functions in PHP:

The built-in functions are those functions which are derived by the PHP designers. And we can just use them whenever

we feel need of them. Because we don’t know which type of programming had been done inside that built-in function,

we just know how to use it and for what we can use it? There are list of built-in functions i.e.

  1. imagecreatetruecolor()
  2. mysql_connect()
  3. phpinfo()
  4. get_loaded_extensions()
  5. str_replace()
  6. usort()
  7. function_exits()
  8. dl()
  9. imagecolorallocate()
  10. imagestring()
  11. imagepng()
  12. imagedestroy()

There are just 11 built-in functions in this lecture, but you can find many built-in functions in PHP. And you can

use them in your dynamic web pages. These built-in functions made easy to design a dynamic website with the

help of PHP programming. And we should keep it in our minds the PHP is a server side language. Check listing 1 for

details about the built-in functions there is an example about the image related function.

Listing 1:what are Builtin Functions in PHP


<?

//creating an image

$ImageToCreate = imagecreate( 270, 80 );

//using color for image background to be fill

$ImageBackground = imagecolorallocate( $ImageToCreate, 128, 255, 0);

//using color for text which to be write on image

$ImageTextColor = imagecolorallocate( $ImageToCreate,0, 0, 255 );

//using underline for text

$ImageUnderLineText = imagecolorallocate( $ImageToCreate, 255, 255, 0 );

imagestring( $ImageToCreate, 16, 30, 25, "Welcome To TrustInGeeks",

$ImageTextColor );

//underline size

imagesetthickness ( $ImageToCreate, 10 );

imageline( $ImageToCreate, 29, 45, 238, 45, $ImageUnderLineText );

//creating the image on the browser

header( "Content-type: image/png" );

imagepng( $ImageToCreate );

imagecolordeallocate( $ImageUnderLineText );

imagecolordeallocate( $ImageTextColor );

imagecolordeallocate( $ImageBackground );

imagedestroy( $ImageToCreate );

?>

builtinfunctions

Figure 1  Output of the Listing 1

User-defined Functions in PHP:

We can also create functions using our own algorithm. And we use functions to avoid writing programming codes

multiple times. For example if we want to display any name ten times as we can use it by loop. But if you don’t know how

to use loop, so write the echo function and print the name ten times. And do this programming in a function. Now

when you need to write those ten names or ten time a single name, so just call the function and then see the program.

From using the built-in functions we can save our time, and also we can write programs very fast. There are four

types of user-defined functions in programming languages commonly. As they are

  1. void FunctionName(void)
  2. void FunctionName(Argument)
  3. Argument FunctionName(void)
  4. Argument FunctionName(Argument)

In PHP programming we use them same as we used user-defined functions in C++ and Java programming Languages.

For details about these check the listing 2, to get idea how to use the user-defined functions in PHP programming.

But in PHP programming we use function keyword to declare or create any function/method.

Syntax:

Function FunctionName(){

Statements…..

}

Listing 2: User-Defined Functions in PHP


<?

$FirstNumber = 10;

$SecondNumber = 10;

$Addition =  AddTwoNumbers($FirstNumber,$SecondNumber);

$Subtraction = SubtractTwoNumbers();

$PrintNumbers = PrintOnScreen($FirstNumber,$SecondNumber);

//Calling the DisplayFunction

DisplayFunction();

echo "<br>Addition of Two numbers are: ".$Addition;

echo "<br>Subtraction of Two numbers are: ".$Subtraction;

function AddTwoNumbers($FirstNumber, $SecondNumber)

{

//the type of this function is Argument FunctionName(Argument)

return $FirstNumber + $SecondNumber;

}

function SubtractTwoNumbers()

{

$New1 = 30;

$New2 = 40;

//the type of this function is Argument FunctionName(void)

return $New1 - $New2;

}

function DisplayFunction()

{

//the type of this function is void FunctionName(void)

echo "<h4>We Are Learning The User-Defined Functions</h4>";

}

function PrintOnScreen($FirstNumber,$SecondNumber)

{

//the type of this function is void FunctionName(Argument)

echo "The First Number is ".$FirstNumber;

echo "<br>The Second Number is ".$SecondNumber;

}

?>

How to Sort Arrays in PHP:

Dealing with Arrays in any programming language is a difficult job. But there are some built-in functions in PHP, by

which we can deal with the arrays fully easy. As we learnt in our previous lecture number 5 about the arrays. There are

about to three types arrays in PHP programming. And how to sort them there are some built-in function. Which made

easy for us to sort the arrays easily. And we use them for different purpose, because when we deal with arrays in PHP

programming. So there are associative arrays too. So it’s not easy to sort them without any function, we can use any

algorithm to use it and make it easy. But we should think about it. It will take long time. To create a new algorithm for

these kind of issues. So let’s we use the built-in functions for sorting the arrays.

  1. ksort()
  2. sort()
  3. arsort()
  4. krsort()
  5. assort()
  6. rsort()

We use these 6 built-in functions to sort the arrays, with different methods. As we use sort() function for ascending

order of arrays. As we use ksort() function for using the key of associative arrays to sort them in ascending form. As we

use arsort() function for using the key of associative arrays to sort them in descending form. As we use krsort() function

for using the key of associative arrays to sort them in descending form. As we use assort() function for using the key

of associative arrays to sort them in ascending form. And as we use arsort() function for using the arrays to sort them

in descending form. See the listing 3 for details about the sorting of arrays in different methods.

Listing 3: Sorting Arrays in PHP


<?

$number = array(0,9,8,7,6,5,4,3,2,1);

$name = array("Tino","Bucky","Ashley","Zeaf");

$record = array("Tino" => "20", "Bucky" => "10", "Ashley" => "23", "Zeaf" => "90");

//using sort function to sort the data in array

sort($number);

for($x=0;$x<count($number); $x++)

{

echo $number[$x]."<br>";

}

//using rsort function to sort the data in array

rsort($name);

for($x=0;$x<count($name); $x++)

{

echo $name[$x]."<br>";

}

//using ksort function to sort the data in array

ksort($record);

foreach($record as $x => $value)

{

echo $value."<br>";

}

//using the arsort funciton to sort the data in array

arsort($record);

foreach($record as $x => $value)

{

echo $value."<br>";

}

//using the krsort function to sort the data in array

krsort($record);

foreach($record as $x => $value)

{

echo $value."<br>";

}

?>

Summary:

In this lecture 6, we learnt about the two types of functions as built-in functions and user-defined functions. And there

are more four types of functions in user-defined functions.

And we learnt about the sorting of arrays in PHP programing using the built-in functions of PHP. And it is easy

to sort the PHP arrays using the built-in functions.

admin
 

student of computer Science , having interest and experience in C++,Java,Php,Html,Css,JavaScript, wordpress and android development