Back to DFS's Workshop Page
Back to Agenda Page


Using a Pair of Functions

Again, your new script will generate the traditional "Hello, World!" greeting. However, this time you will store the pair of functions in a separate file. If you are creating a large web site, this technique allow you to have a degree of uniformity, because the scripts for any of the pages can call on the functions without redefining them. Follow the simple steps below.

  1. Get to the directory where you are storing your PHP code.

  2. Create a new file called helloworld5.php so that it looks like the following code.

    <?php
    // PHP echoing "Hello World in HTML"
    // Using functions in the file commonhtml.inc
    // helloworld5.php
    require ('commonhtml.inc');
    
    $title = "Hello Using Functions";
    htmlheader($title);
    echo "  <p>Hello, World!</p>\n";
    htmlfooter();
    ?>
    
    
  3. In the same directory, create a file called commonhtml.inc in which you will have the definitions of the two functions.

    <?php
    // Functions for HTML tasks
    // commonhtml.inc
    function htmlheader ($t)
    {
      echo "<html>\n";
      echo " <head>\n";
      echo "  <title>" . $t . "</title>\n";
      echo " </head>\n";
      echo " <body>\n";
    }
    function htmlfooter ()
    {
      echo " </body>\n";
      echo "</html>\n";
    }
    ?>
    
  4. In your favorite web browser, type

    http://localhost/~YOURID/test_php/helloworld5.php
    where YOURID is the user ID you have on the system.

Notes


© 2003-2004 DFStermole
Created: 4 May 03
Last Modified: 13 Mar 2004