PHP Tutorials – Functions (Part I)
In this PHP Tutorial you will learn about PHP Functions – Part 1, Function syntax, example, parameters and variable scope.
Sometimes you need to perform an operation more than once in the code, to do so, you can use functions,
A function is declared using the following syntax:
function function_name([parameters])
The name of the function must follow the rules of the identifier, because it’s an identifier that identifies this function.
Function can have zero to many parameters, parameters are used as variables thought the function code block.
The function code block starts with a curly brace, and ends with a curly brace.
Example:
<?php
function get_days($week) {
return $week * 7;
}
$weak = 15;
echo get_days($week);
?>
The code is to get the number of days of the year until the end of this week, to calculate the number of days, you have to know the week number and then multiply it by 7, so multiplying the week number by 7 won’t be changed, and it can be used many times to get the number of days for any week, and so a separate function is made to calculate the number of days.
In order for the function to be able to calculate the number of days, it needs to know the week number, so it’s a parameter for the function, no one can call this function without passing the week number.
A new feature of functions is the ability to return values, as we can see here the get_days function made the calculation and returned the calculation result, so it can be used by the script.
The script defined a value for the week number and called the function using the week number, the function returned the number of days, and the script printed it.
The variable name can be different than the function parameter name, the function parameter name is just to tell that to use this function, you have to pass parameters to it.
In the previous example, the $weak value is passed to the function, only the value, so if we printed the variable $weak after its value was passed to the function, it would print 15.
Functions can assign default values for any of its parameters, so if user didn’t pass a value for that parameter, it will use the default value.
Example:
<?php
function get_days($week = 1) {
return $week * 7;
}
echo get_days();
?>
No parameters were passed to the function, so it will use the default value, which is 1, and will return 7.
Variables scope:
Variables defined out of any functions are called global variables, for most programming languages, global variables are available for functions, so functions are able to changes their values freely.
This is not the case for PHP, functions can’t access global variables directly.
Example:
<?php
$week = 12;
echo get_days();
function get_days() {
$weeks = $week + 3;
return $week * 7;
}
?>
$week inside the function is different than the global $week, it’s not initialized at the function, so it will be substituted by NULL, and will return zero when multiplied by 7.
If you want to use the global variables inside functions, you have to use the global statement.
Example:
<?php
$week = 12;
echo get_days();
function get_days() {
global $week;
$weeks = $week + 3;
return $week * 7;
}
?>
This way the function will use the global $week, and will return 105.
Any operation on the global variable will affect it’s value, so after this function the global variable value will be 15.