Php Function

PHP functions are comparable to other programming languages. A function is a portion of code which takes one more input in the form of parameter and does some processing and returns a value.

PHP have already built-in functions but PHP gives you option to create your own functions as well.

There are two parts :

Creating a PHP Function
Calling a PHP Function


PHP User Defined Functions

Php user defined function is a block of statements that can be used repeatedly in a program,It will not execute immediately when a page loads,It will be executed by a call to the function.

Creating PHP Function:

Its very easy to create your own PHP function. Suppose you want to create a PHP function which will simply write a simple message on your browser when you will call it. Following example creates a function called writeText() and then calls it just after creating it.

Note that while creating a function its name should start with keyword function and all the PHP code should be put inside { and } braces as shown in the following example below:

Example:

<?php
function writeText()
{
      echo “Hello world!”;
}
writeText();     // calling function
?>
Output:
Hello World!



PHP Functions with Parameters/Arguments:

PHP gives you option to pass your parameters inside a function. You can pass as many as parameters/arguments your like just seperate them with a comma. These parameters work like variables inside your function. Following example takes two integer parameters and add them together and then print them.

Example:

<?php
function addFunction($number1, $number2)
{
      $sum = $number1 + $number2;
      echo “Sum of the two numbers is : $sum”;
}
addFunction(10,20)    // calling function
?>
Output:
Sum of the two numbers is : 30



PHP Default Argument/ Parameter Value :

You can set a argument to have a default value if the function’s caller doesn’t pass it.

Following function prints NULL in case use does not pass any value to this function.

Example:

<?php
function setNumber($num=100)
{
      echo “The number is : $num”;
}
setNumber(20);
setNumber();            // will use the default value of 100
setNumber(60);
?>
Output:
The number is : 20
The number is : 100
The number is : 60



PHP Functions retruning value :

A function can return a value using the return statement in combination with a value or object. return stops the execution of the function and sends the value back to the calling code.

You can return more than one value from a function using return array(1,2,3,4).

Following example takes two integer parameters and add them together and then returns their sum to the calling program. Note that return keyword is used to return a value from a function.

Example:

<?php
function sum($p,$n)
{
        $r=$p+$n;
        return $r;
}
$return_value = sum(20,30)
echo “Returned value from the function : $return_value”
?>
Output:
Returned value from the function : 50



PHP Dynamic Function Calls :

It is possible to assign function names as strings to variables and then treat these variables exactly as you would the function name itself. Following example depicts this behaviour.

Example:

<?php
function abc()
{
        echo “Hello<br />”;
}
$function_holder = “abc”;
$function_holder();
?>
Output:
Hello

No comments:

Post a Comment