Changes

Jump to: navigation, search

PHP Functions

1,023 bytes added, 20:04, 31 May 2007
Returning a Value from a PHP Function
== Returning a Value from a PHP Function ==
 
A single value may be returned from a PHP function top the script from which it was called. The returned value can be any variable type of your choice.
 
The ''return'' keyword is used to return the value:
 
<pre>
<?php
function returnTen ()
{
return 10;
}
?>
</pre>
 
The above example returns the value of 10 to the calling script.
 
== Passing Parameters to a PHP Function ==
 
Parameters (or ''arguments'' as they are more frequently known) can be passed into a function. There are no significant restrictions of the number of arguments which can be passed through to the function.
 
A function can be designed to accept parameters by placing the parameter names inside the parentheses of the the function definition. Those variables can then be accessed from within the function body as follows:
 
<pre>
<?php
function addNumbers ($val1, $val2)
{
return $val1 + $val2;
}
?>
</pre>
 
In the above example the ''addNumbers()'' function accepts two parameters as arguments, add them, together and returns the result.

Navigation menu