Page 85 - Open Soource Technologies 304.indd
P. 85
Unit 4: Functions
• Where function_name is the name of your function (a PHP function name must have the Notes
same form as a PHP variable name, the only difference being that you must not put a dollar
symbol in front of it)
• where the $par_i denote parameters that are accepted by the function (its arguments, in
other words)
• where php_instructions is the set of PHP instructions that must be executed by the function.
• Unlike PHP variable names, PHP function names are case-insensitive. It is however
recommended to remain rigorous when naming and calling any PHP object (remember
what we said about how easy it is to make errors in loosely typed programming languages).
• A function can be defined within another function.
• Every function has a global scope (even if it was defined within another function).
Let’s take a look at a simple function. For example takes two strings, concatenates them, and then
returns the result (in this case, we have created a slightly slower equivalent to the concatenation
operator, but bear with us for the sake of example).
Example: String concatenation
Function strcat($left, $right) { $combined_string = $left . $right; return $combined_string; }
The function takes two arguments, $left and $right. Using the concatenation operator, the function
creates a combined string in the variable $combined_string. Finally, in order to cause the function
to have a value when it is evaluated with our arguments, we return the value $combined_string.
Because the return statement can accept any expression, even complex ones, we can simplify the
program as shown in Example:
Example: String concatenation redux
function strcat($left, $right) { return $left . $right; }
If we put this function on a PHP page, we can call it from anywhere within the page. Take a look
at Example:
Example: Using our concatenation function
<?php function strcat($left, $right) { return $left . $right; } $first = “This is a “; $second = “ complete
sentence!”; echo strcat($first, $second); ?>
When this page is displayed, the full sentence is shown.
This function takes in an integer, doubles it, and returns the result:
function doubler($value) { return $value << 1; }
Once the function is defined, you can use it anywhere on the page. For example:
<?= ‘A pair of 13s is ‘ . doubler(13); ?>
You can nest function declarations, but with limited effect. Nested declarations do not limit the
visibility of the inner-defined function, which may be called from anywhere in your program.
The inner function does not automatically get the outer function’s arguments. And, finally, the
inner function cannot be called until the outer function has been called.
4.2 Calling a Function
Functions in a PHP program can be either built-in (or, by being in an extension, effectively built-
in) or user-defined. Regardless of their source, all functions are evaluated in the same way:
$some_value = function_name( [ parameter, ...] );
The number of parameters a function requires differs from function to function. The parameters
supplied to the function may be any valid expression and should be in the specific order expected
LOVELY PROFESSIONAL UNIVERSITY 79