Functions in PHP

Functions in PHP


Posted in : PHP Posted on : January 3, 2011 at 5:52 PM Comments : [ 0 ]

This section contains the detail about Functions in PHP.

Functions in PHP

A function is the a block of code whom you can name according to your choice. These functions can be executed whenever we need it.

To protect the script from being auto executed on page load , we put the code into a function. A function will only be executed by a call to the function. You may call these function from any where within page.

Syntax of the function is given below :

function functionName()
{
code to be executed;
}

Given below the example :

<html>
<body>

<?php
function PrintName($name)
{
echo $name;
}

echo "Setting name value.The name is ";
PrintName("Kapil Singh");
?>

</body>
</html>

Output :

Setting name value. The name is Kapil Singh

Return value by functions

If you want that your function should return a value, use the return statement. Given below the example :

<html>
<body>

<?php
function subtraction($a,$b)
{
$x=$a-$b;
return $x;
}

echo "6 - 3 = " . subtraction(6,3);
?>

</body>
</html> 

Output :

6 - 3 = 3

Go to Topic «PreviousHomeNext»

Your Comment:


Your Name (*) :
Your Email :
Subject (*):
Your Comment (*):
  Reload Image
 
 

 
Tutorial Topics