5.3: Functions

Section 5.3 PHP Functions

  • Functions are an integral part of php development. We use functions for a collection operations we need to use repeatedly in our php program.

  • Let's take a look at Figure 5.3.5. There's a lot going on in this figure so let's break it down piece by piece.

Figure 5.3.1

Item 1 is an open php tag

Item 2 declares a variable named $thePerformer

  • As we saw earlier, variables are a simple container used to store data that will then be used elsewhere in the php code.

  • In this case the container is called $thePerformer and the text "David Bowie" is being placed in the container.

  • Note that semi-colon at the end of this line. A php program might have dozens of statements that tell the program what do. The semi-colon is used to tell php that a particular statement is complete.

  • For example:

<?php
echo "This is an example of stacking up a few php statements";
echo "A php program may have several statements like this one";
echo "We could make this example very very tedious if we liked?";
?>

Item 3 is the closing php tag ?> that lets the php interpreter know that, for now, there is nothing else to process.

  • We can see that immediately after our closing tag we have some HTML consisting of an h3 tag.

  • If we had not used the php closing tag the server would have thrown an error and processing the page would stop.

  • An error code would be displayed in the visitors browser; we don't want our site visitors to ever see an unhandled error message returned when they ask for information.

Item 4 is a function call with a parameter. Let's break this line down left to right.

  • The line starts off with an html paragraph tag.

  • The paragraph tag is followed by an open php tag <?php signaling the server that php code follows and it is time to start interpreting php commands.

  • The "signature" of the desired function we are calling is next: printPerformerBiography($thePerformer) ?>

  • Let's think in terms of an entertainment agency

    • Think of the Diamond Dog Entertainment Website as a PHP application.

    • Our agency might have dozens of functions that can be called. There may be functions to print a biography, create a list of stops on an upcoming tour, use that list and print an itinerary for the tour, list merchandise for various entertainers such as t-shirts, buttons and music.

    • Think about a site visitor looking for biography information about a particular artist.

      • Your site visitor selects an entertainer from a list of those entertainers that Diamond Dog represents.

      • The site visitor clicks a link or button or a menu-item to show entertainers biography.

      • The application passes the entertainer's name to a function that expects and entertainer's name.

Item 5 is the function signature along with its passed parameter.

  • We'll first discuss the function signature

  • The word function tells php that the code that follows will be treated as a collection of statements.

  • Our function is named printPerformerBiography($selectedPerformer) and it expects the caller to pass it a performer's name.

    • Next we see an opening brace "{" This brace tells php that a collection of php statements follows.

    • There is only one statement in our function but there could be many, each ending in semi-colon. This statement simply echoes the performers name with some static text about how he or she got started.

    • Next we see a closing brace "}" to let php know there are no more statements

    • Processing returns to the line of code just below where the function was called.

    • In our example we see the line with the php closing tag ?>

Item 6 is the placeholder for the value that was passed as a parameter in the function call. Note that the parameter name in the function signature does not have to match the name of the parameter being passed. What is important is the data type and the actual value of the parameter not the variable name.

  • Save these changes, return to the browser and refresh the Diamond Dog Entertainment page. Your page should look something like this:

Figure 5.3.2

Last updated