PHP Functions and Return values


 

PHP Functions and Return values

This section contains the detail about the PHP Functions and Return values in PHP.

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

Ads