PHP DevCenter
oreilly.comSafari Books Online.Conferences.


PHP Foundations

An Introduction to Functions, Part 1


Today we'll introduce the function declaration, how it works, and how it can be used to create your own custom functions in PHP 4.

What are functions? A function is basically a compartmentalized PHP script designed to accomplish a single task (usually a task that will need to be accomplished multiple times). Furthermore, code contained within functions is ignored until the function is called from another part in the script.

We've already worked with some functions such as the count() function which returns the number of elements within an array. Today we'll introduce the syntax of a function in PHP and use what we have already learned to demonstrate how functions can be used to save time and effort.

Declaring your functions

Creating a custom function in your scripts is a fairly straightforward concept. All functions in PHP begin with the keyword function followed by the function name. This function name must adhere to the same criteria as variables except they do not begin with $ character as other PHP variables do. Directly after the function name follows a set of parentheses containing the parameters to be passed. Before we discuss parameters, let's examine the formal declaration of a function:

function  <name>([$var1 [= constant]], 
                 [$var2 [= constant]], ...) {

where name represents the function name, and between the parentheses is contained an optional set of variable parameters to be passed to the function. Because these parameters are optional, we'll discuss functions without them first.

Our first function

Now that we have an idea of what goes into the function, let's create our first basic function and demonstrate how to call the function from your script. In our function, we'll echo the string "Hello, PHP Functions!" 10 times and then show how functions affect the way the PHP code is interpreted:

  function myfunction() {
    for($i = 0; $i < 10; $i++) {
      echo "Hello, PHP Functions!<br />";

  echo "This is before the function is called<br />";
  echo "This is after the function has been called";
  echo "<br />";

The output to the browser when this script is executed is:

This is before the function is called
Hello, PHP Functions!
... (nine more iterations)
This is after the function has been called

Also in PHP Foundations:

Using MySQL from PHP, Part 2

Using MySQL from PHP

MySQL Crash Course, Part 3

Passing parameters to functions

Now that we've covered functions that contain no parameters, we'll discuss what it means to pass parameters to a function in PHP and go into the details of how to do it from within your scripts. A function parameter is nothing more than a piece of data that the function requires to execute. For instance, the function count() requires that an array is passed to it -- if not, what could it count? As per our formal definition, function parameters are represented by variable names located within the parentheses of the function definition. For instance, the following is an example of our previous function with one distinct difference -- we now will be able to specify how many times our message will be displayed and the text of the message from outside of the function:

  function myfunction($num, $msg) {
    for($i = 0; $i < $num; $i++){
      echo $msg . "<br />";
  echo "Printing the message 5 times....<br />";
  myfunction(5, "This is the message");

Default parameter values

Normally, when parameters are declared as part of the function declaration they become a required part of the syntax when the function is called. That is, you can not call myfunction() with no parameters if the function itself requires them. Fortunately, PHP supports the ability to assign default values to function parameters. For example, we can rewrite our function a third time to include a default parameter for the message:

  function myfunction($num, $msg = "Default Message") {
    for($i = 0; $i < $num; $i++) {
      echo $msg . "<br />";

  echo "Displaying the default message 5 times<br />";
  echo "Displaying a custom message 6 times<br />";
  myfunction(6, "My custom message");

Through this method, you can create more versatile functions that allow you to make assumptions on the data being passed to the function without sacrificing flexibility.

Final notes

With that, we'll conclude today's introduction to functions. Although we have covered the majority of the topic of functions in PHP, there are still more topics to be addressed in a future article. Fortunately, our introduction should be more than enough to get started using this most powerful and time-saving feature of PHP. Happy coding!

John Coggeshall is a a PHP consultant and author who started losing sleep over PHP around five years ago.

Read more PHP Foundations columns.

Return to the PHP DevCenter.

Valuable Online Certification Training

Online Certification for Your Career
Earn a Certificate for Professional Development from the University of Illinois Office of Continuing Education upon completion of each online certificate program.

PHP/SQL Programming Certificate — The PHP/SQL Programming Certificate series is comprised of four courses covering beginning to advanced PHP programming, beginning to advanced database programming using the SQL language, database theory, and integrated Web 2.0 programming using PHP and SQL on the Unix/Linux mySQL platform.

Enroll today!

Sponsored by: