PHP Tutorial – Functions Parameters and Return Values
In a previous tutorial we looked at how to make your own PHP functions. In this PHP tutorial we will see how to use function parameters (for example passing a variable to a function) and function return values.
Functions Parameters
Parameters are specified after the function name, inside the parentheses.
Let’s look at a function parameter example:
<html>
<body>
<?php
function ourFunction($x)
{
echo $x . ".<br />";
}
$y = “black”;
echo "My car color is ";
ourFunction("white");
echo "My car color is ";
ourFunction ("$y");
?>
</body>
</html>
The result of the example will be:
My car color is white.
My car color is black.
You can pass multiple parameters by using a comma between the parameters, for example:
function MyFunction( $X, $Y, $Z)
{
code to be executed;
}
Function Return Values
Sometime you want to return a value if you are using functions. This is where the return statement comes in. Let’s look at an example:
<html>
<body>
<?php
function addValues($x,$y)
{
$total=$x+$y;
return $total;
}
echo "2 + 2 = " . addValues(2,2);
?>
</body>
</html>
You can guess the result. We pass two values to the function addValues. In the function we add the two numbers and return the total.
That’s all for this PHP tutorial.
Thanks ..
its very to learn here…..
thanks for this tutorial. it help’s me to learn php.
thanks…
its very clear explanation…