Defining functions on PHP is one of the simplest things that can be done. Functions are a great way to control your code and, when used correctly, can provide some great power to it as well.
Not likely C++ (and some other languages) that you defined the function output just before the function name, PHP works with a simple definition where you define the name and arguments that will be used to define the function and it’s scope.
As mentioned before defining a function with PHP is simple:
function userDefinedPrint ()
echo ‘My first function … sweeeetttt … ‘;
PHP functions does not carry a need for a argument, return or even needs to be declared on the top of the code (safe a exception – check example 2) to be called, which make them pretty flexible to work with.
The real major restrictions of defining a user-defined function in PHP is the same for any variable declaration or any label in PHP, function names must start with an underscore or a letter, never a number, the functions cannot, at any instance have the same name as a PHP function or structure and the names are case sensitive. Defining a function called print, would cause an error on PHP that’s because PHP does not support function overloading.
There is three basic ways that you can pass an argument to a user-defined function:
- By value and this one being required or optional
- By reference
- By argument array
When defining a function that needs to pass arguments by value or, better explaining, using a variable that will be used inside the function scope to receive a value, it’s the most common form to do it where the only thing that needs to be observed is if the value is required or optional.
function userDefinedPrint($whatINeedToPrint, $maybeThisToo = ”)
if (strlen($maybeThisToo) > 0)
userDefinedPrint(‘my first function… ‘);
/** this will output: my first function… */
userDefinedPrint(‘my first function… ‘ , ‘yeayyyyy’);
/** this will output: my first function… yeayyyyy */
The function above have 2 arguments, one required and another one optional. Whenever the function is called it requires to give at least one argument, and, by function scope, if the second argument is given (a condition that it’s size is bigger than 0), then it will also be printed.
The second argument, the optional one, is called optional because it already have it’s default value given, therefore the function will always know that if the second argument is not given, it will use the default value on it’s scope.
The second most used type of function is creating a one which receives the parameter by reference. Passing a parameter by reference is quite tricky thing if the concept is not fully understood.
When creating a function that uses a parameter by reference, automatically PHP knows that the programmer is referencing a variable that is outside the function scope.
A nice way to place this concept is the same as the “pointers” used by C++. In this case the function parameter “points” to the same memory area as the previously defined variable.
In order to define a parameter as a referenced parameter you must use the ampersand (&) before the variable name
$referencedVariable = 0;
echo $referencedVariable; // will display 0
echo $referencedVariable; // will display 1
Is important to notice that it’s necessary the full understanding of the “reference” concept. If wrongly used, it will surely return unexpected results, therefore, when well used, it surely can improve the code programming logic.
The last item is to define a function using an argument array (or a variable length argument list). It isn’t a hard concept to understand, but instead of creating a function that will show that, I will use one, very well know one, that can exemplify this concept in a beautiful way.
The “echo” constructor does not require for you to pass the arguments between parenthesis. While using this constructor it pass one or more arguments to print.
echo ‘this is the string I want printed’;
echo ‘this is the’ , ‘ string ‘ , ‘ I want printed’ ;
The result for both calls is the same, what changes is the number of arguments. In the first call “echo” only have one single argument. On the second call “echo” does have 3 arguments (separated by commas).
On the second call the “echo” constructor will first print the first argument, then print the second and third in order. When used some concatenation or any mathematical operation, it will work on those first and then start the output (check the echo reference guide for more information).
To define a function that use a variable length argument list you need to know how many arguments are (func_num_args()) and how to get the argument value (func_get_arg(), and func_get_args()). This surely increases the level of understanding of the function, but, in some cases (let’s say when working for a script that is executed by a cron job), it’s extremely helpful.
Defining a function that uses a variable argument list is simple, just don’t set a number of arguments or parameters on the function scope (leave it blank), but knowing how many arguments and getting the value of each argument can be tricky and that’s why PHP offer the argument function sisters:
Here is a simple example that will help understanding the creation of a function like this:
$num_arguments = func_num_args();
$email = func_get_arg(0);
$subject = func_get_arg(1);
$body = func_get_arg(2);
if ($num_arguments > 1)
// you could use as well the func_get_args array;
emailBlaster(‘firstname.lastname@example.org’,’testing’,’this is the email body’);
The variable length argument array does not have a defined number of arguments and it should be used only on very specific situations.
About functions pretty much everything is covered and all that needs to be done now is to improve and better understand how each type works and what is the best way to use each type.
Play a bit, check the results, check the PHP manual, have fun.