Introduction to PHP for Os X by Example
Functions
- To define a basic function we use the following syntax .
function function_name(){
function code;
}
- If a function is to have invocation arguments we name their associated variables in the function definition as follows :
function function_name($inv_arg_var) {
- Invocation argument variables can have default values like so:
function function_name($inv_arg_var='default_val') {
- All invocation argument variables that have a default value should be at the end of the list of variables and all that don’t should be at the start.
- Variables within a function are scoped to that function. That is they do not exist outside the function. The reverse goes for variables we create outside the function.
- To use a variable from outside a function inside it we can use the key word global or better still the $GLOBALS array
- To invoke a function we use this syntax:
function_name();
- Invoking a function with invocation arguments is just as straight forward:
function_name('inv_arg_var');
- To return a value from a function use the keyword return.
- To assign the value returned from a function, to a variable, use the following syntax:
$var_name = function_name('inv_arg_var');
Resources