Now: Tutorial for Web and Software Design > PHP > PHP Basic > PHP Content
> An Introduction to Functions, Part 1 [Bookmark it]
An Introduction to Functions, Part 1
PHP Foundations

An Introduction to Functions, Part 1

07/12/2001

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:

<?php

  function myfunction() {

    for($i = 0; $i < 10; $i++) {

      echo "Hello, PHP Functions!<br />";

    }

  }



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

  myfunction();

  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

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:


<?php

  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:

<?php

  function myfunction($num, $msg = "Default Message") {

    for($i = 0; $i < $num; $i++) {

      echo $msg . "<br />";

    }

  }



  echo "Displaying the default message 5 times<br />";

  myfunction(5);

  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.

[Bookmark][Print] [Close][To Top]
  • Prev Article-PHP:

  • Next Article-PHP:
  • Related Materias
    Scaling Dynamic Websites w
    Whats New in ModSecurity
    A Day in the Life of #Apac
    Important Notice for Apach
    Custom-Compiling Apache an
    Apaches eXtended Server Si
    A Day in the Life of #Apac
    A Day in the Life of #Apac
    A Day in the Life of #Apac
    A Day in the Life of #Apac
    Topics
    Photoshop Tutorial
     

    Special Effect

      3D Effect
      Photoshop Articles
    Programming Tutorial
     

    C/C++ Tutorial

      Visual Basic
      C# Tutorial
    Database Tutorial
     

    MySQL Tutorial

      MS SQL Tutorial
      Oracle Tutorial
    Graphic Design Tutorial
     

    Coreldraw Tutorial

      Illustrator Tutorial
      3D Graphics Articles
    Webmaster Articles
     

    Domain Service

      Web Hosting
      Site Promotion
    Java Tutorial&Articles
     

    Java Servlets

      JavaEE Tutorial
     

    JavaBeans Tutorial

    XML Tutorial&Articles
     

    XML Style Tutorial

      AJAX Tutorial
      XML Mobile
    Flash Tutorial&Articles
     

    Flash Video

      Action Script
      Flash Articles
    OS Tutorial&Articles
     

    Linux Tutorial

      Symbian Tutorial
      MacOS Tutorial