How to Build Your Own PHP Functions - dummies

By Richard Blum

While you’re coding in PHP, you’ll often find yourself using the built-in functions available (such as the rand() function you used earlier in the example programs). Functions are nothing more than PHP code someone else wrote to accomplish a useful feature that you can use in any program. Instead of having to copy all the code into your application, you just use the function name.

PHP allows you to create your own functions to use in your programs and share with others. After you define a function, you can use it throughout your program. This saves typing if you use a common routine or block of code in lots of places in your application. All you need to do is write the code once in the function definition and then call the function everywhere else you need it.

The basic format for a function definition looks like this:

function name(parameters) {
    function code
    return value;

The name must uniquely identify the function. It can’t be one of the existing PHP function names, and it can’t start with a number (although numbers can appear anywhere else in the function name).

The parameters identify one or more variables that the calling program can pass to the function (or you can have a function that requires o parameters). If there is more than one variable in the parameter list, you must separate them with commas. You can then use the variables anywhere within the function code, but they only apply to inside the function code block. You can’t access the passed parameter variables anywhere else in the program code.

Any variables you define inside the function code apply only to the function code. You can’t use function variables in the PHP code outside the function definition.

The return statement allows you to pass a single value back to the calling program. It’s the last statement in the function definition code, and it returns control of the program back to the main code section in your program.

Try out the following steps to experiment with creating a function and using it in your PHP program:

  1. Open your editor and type the following code:
    <!DOCTYPE html>
    <title>PHP Function Test</title>
    function factorial($value1)
        $factorial = 1;
        $count = 1;
        while($count <= $value1) {
            $factorial *= $count;
        return $factorial;
  2. Save the file as factest.php in the DocumentRoot folder for your web server.
  3. Test your code here.
  4. Close your browser when you’re done.

When you run the factest.php program, the output should look like what you see below.

factest.php program
The output from the factest.php program.

All the code required to calculate the function is contained within the factorial() function definition code block. When PHP uses the factorial() function, it passes a single value that the function assigns to the $value1 variable. When the calculation is complete, the function code returns the results back to the main program.

The main program uses the factorial() function twice in the code, both embedded in echo statements:

echo "The factorial of 10 is " . factorial(10) . "<br>\n";
echo "The factorial of 5 is " . factorial(5) . "<br>\n";

You can embed variables inside the string values in echo statements, but you can’t embed functions. To insert the output from the function into the echo statement output, the code uses the string concatenation operator (the period) to “glue” the output from the strings and the factorial() functions into a single string to display.

If you have lots of functions that you use in many of your programs, you can define them in a separate file. Then to use the functions in your programs just use the include() function to include the function file, and you can then use the functions inside your programs without having to retype them!