📖 PHP Functions

Functions

A function is a group of statements treated as a single unit. Functions allow you to break your code into smaller, more manageable units. A function should perform a single task. To break your code into functions, consider grouping statements that work together to accomplish one task. This is called encapsulation.

Functions may be called from anywhere in your code making them reusable. Functions act like a variables with the function name being the variable and the body of the function is the value.

Create a function definition.
<?php
function function_name(parameters) {
	PHP statements;
}
?>
Rules for Functions
  • Reserved word function is required
  • function_name identifies the function. Rules same as for variables
  • () required, even if no parameters.
  • Parameters are optional. If used, they are arguments or data values that are passed to the function from the calling procedure. Multiple parameters are separated by commas. (param1, param2, param3)
  • The PHP statements that compose the function are enclosed in curly brackets {}
  • Functions may be placed anywhere in your code
  • The PHP statements inside a function are not executed until the function is called
  • To call a function:
    function_name(parameters);
    or
    $x = function_name(parameters);
  • Parameters may be variables or values.
  • Functions may or may not return a value.
Passing values to a function.
  • To pass a value to a function, you simply put it in the parentheses after the function: i.e. myFunction(argument1, argument2, ...) These are called arguments or parameters.
  • You can have any number of arguments for a function (including 0).
  • When passing arguments, you may use a variable name or an actual value (i.e. myFunction($count, 10)
  • The function definition must include variable names for each argument that is passed to it. (i.e. myFunction($arg1, $arg2, ...). These act as local variables within the function.
  • The number of arguments in the function definition must match the number of arguments in the function call statement.
  • The names of the argument variables in the function definition do not have to match the names of the variables in the function call statement.
Returning values from a function.
  • To return a value to a function, you simply add a return statement to the function with a value: return $var1;
  • To be able to use the value returned, you must assign the function call to a variable: $ans = myFunction($arg1, $arg2);
  • You can return a literal value, a variable or an expression.
  • A function can return only one value.
  • The return statement can be placed anywhere within a function, but once executed, the script exits from the function.
  • You can also use the return statement without any data to end a function early.
Example of a function that receives and returns values.
<?php
function calcAvg($val1, $val2, $val3) {
	$avg = ($val1 + $val2 + $val3) / 3;
	return $avg;
}

$ans = calcAvg(10, 20, 30);
print "<p>The average is $ans</p>";
?>

The function has 3 arguments:$val1, $val2, and $val3. When we called the function, we passed it the numbers 10, 20 and 30. This assigned $val1 a value of 10, $val2 a value of 20 and $val3 a value of 30. The function calculated the $avg (10 + 20 + 30) / 3 which equals 20 which it stored the $avg variable. This value was returned to the calling statement so that at the end of the statement, the variable $ans would equal 20.

Variable Scope
Global variable
declared outside a function and available to all parts of your program
  • in order to use a global variable inside a function, you must declare it using the global keyword inside of the function. You do not need to assign a value as it will get the value from the existing global variable
  • global variables should only be used where necessary - i.e. where the data is needed outside of a single function
Local variable
declared inside a function and only available within that function
  • local variables are created inside of a function and are therefore only available to that function
  • function arguments behave like local variables
  • local variables are more efficient than global variables and should be used wherever possible
Example of a function that uses global variables.
<?php
$val1 = 10;
$val2 = 20;
$val3 = 30;

function calcAvg() {
global $val1;
global $val2;
global $val3;
global $ans;
$ans = ($val1 + $val2 + $val3) / 3;
}

calcAvg();
print "<p>The average is $ans</p>";
?>