Changes

PHP Functions

30 bytes added, 15:08, 13 May 2009
no edit summary
== What is a PHP Function? ==
<google>ADSDAQBOX_FLOW</google>
Functions are basically named scripts that can be called upon from any other script to perform a specifc specific task. Values (known as ''arguments'') can be passed into a function so that they can be used in the function script, and functions can, in turn, return results to the location from which they were called.
PHP functions exist it two forms, functions that are provided with PHP to make your life as a web develeper developer easier, and those that you as a web developer create yourself.
== How to Write a PHP Function ==
The first step in creating a PHP function is to provide a name by which you will reference the function. Naming conventions for PHP functions are the same as those for variables in that they must begin with a letter or an underscore and must be devoid of any kind of white space or punctuation. You must also take care to ensure that your function name does not conflict with any of the PHP built in functions.
PHP functions are created using the ''function'' keyword followed by the name and, finally, a set of parentheses. The body of the function (the script that performs the work of the function) is then enclosed in opening and closing braces.
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>
When loaded into a browser the ''print'' statement will display the result returned by the addNumbers() function, in this case the number 30.
== Passing Parameters By by Reference ==
In the above example we only passed the values contained in $var1 and $var2 into the addNumbers() function. Had we made any changes to these values in the function the original variables would have remained unchanged. Suppose we change our script so that the function adds 10 to each of the parameters passed in. If we then check the values of the $var1 and $var2 variables after the call to the function we will see they they have not been changed:
<pre>
</pre>
If we wanted the script to work on the actual variables themselves, and change the values of those variables then we do something called ''passing by reference''. This has the effect of passing a reference to the actual variable rether rather than simply passing the value assigned to the varaiblevariable. We specify that we are passing by reference by prefixing the variable names in our function definition with an '&' character. If we modify our previous script so that variables are passed by reference:
<pre>
</pre>
== Returning Values By by Reference ==
In addition to passing arguments into a PHP function by reference, it is also possible to return a result by reference. To achieve this , the function name must be prefixed with the '&' character in the function defintion definition and, also at the point at which it is called. In the following example the ''addNumber()'' function returns the result by reference:
<pre>
Global myString = global string
</pre>
 
<google>BUY_PHP_BOTTOM</google>