Home Tutorial Php Phpbeginners PHP Functions and Return values

 
 

PHP Functions and Return values
Posted on: February 5, 2011 at 12:00 AM
Advertisement
This section contains the detail about the PHP Functions and Return values in PHP.

PHP Functions and Return values

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

Advertisement

Related Tags for PHP Functions and Return values:


Follow us on Twitter, or add us on Facebook or Google Plus to keep you updated with the recent trends of Java and other open source platforms.

Posted on: February 5, 2011

Recommend the tutorial

Advertisements Advertisements
 

 

 

DMCA.com