PHP Function

In this tutorial you will come to know about functions in PHP.With the help of functions we can reduce time and effort, all we need to do is create a function and put the necessary coding into it and we can call this function from anywhere of the program.

 
 

Share on Google+Share on Google+

PHP Function

Ads

In this tutorial you will come to know about functions in PHP.With the help of functions we can reduce time and effort, all we need to do is create a function and put the necessary coding into it and we can call this function from anywhere of the program.

Sometimes we need one set of code more than one time, at that point of time function helps us a lot. A function is a set of coding within a block, which has a specific name, could have few arguments, and a return data type.

With the help of functions we can reduce time and effort, all we need to do is create a function and put the necessary coding into it and we can call this function from anywhere of the program.

One function can be called number of times, and one function can call itself too, this technique is called recursion. 

Example:

<?php

function add($a,$b){

return $a+$b;}

$c=add(12,39);

echo "Addition is:".$c;

?>

 

Output:

Addition is:51

 

Example:

<?php

function fact($b){

if($b==1)

return 1;

else

return($b* fact($b-1));

}

echo "Factorial of 6 is:".fact(6);

?>

Output:

Factorial of 6 is:720

More PHP Functions with examples

Advertisements

Posted on: February 17, 2010 If you enjoyed this post then why not add us on Google+? Add us to your Circles