PHP: Libraries

Derek Bridge

Department of Computer Science,
University College Cork

PHP: Libraries

Aims:

Headers and footers

Example of simple header and footer

Functions for headers and footers

Definition of a simple output_header function

function output_header( $title, $stylesheet )
{
    echo 
        '<!DOCTYPE html>
         <html lang="en">
         <head>
         <meta charset="utf-8" />';
    echo "<title>{$title}</title>";
    echo "<link rel=\"stylesheet\" href=\"{$stylesheet}\" />";
    echo 
        '</head>
         <body>';
    echo "<h1>{$title}</h1>";
}

Example of a typical web page, e.g. alchemy.php

<?php
    require_once('output_functions.php');
    output_header( 'Department of Alchemy', 'nerdland.css' );
?>

    <p>
        Welcome to the Department of Alchemy at the National University 
        of Nerdland! We're great, we are.
    </p>
    
<?php
    output_footer( 'National University of Nerdland' );
?>
    

Libraries

Definition of an output_submit_button function

function output_submit_button( $button_label ) 
{
    echo "<input type=\"submit\" name=\"submit\" value=\"{$button_label}\" />";
}

This is pockets.php, the new version of pockets.html

<?php
    require_once('output_functions.php');
    output_header( "What's in your pocket?", 'pockets.css' );
?>

    <form action="process.php" method="get">
<?php
    output_textfield('firstname', 'Firstname: ', 'firstname', 25, 25, '', false);
    output_textfield('surname', 'Surname: ', 'surname', 25, 25, '', false);
    output_textfield('num_1c_coins', 'One cents: ', 'num_1c_coins', 3, 3, '0', false); 
    output_textfield('num_2c_coins', 'Two cents: ', 'num_2c_coins', 3, 3, '0', false);
    …
    output_textfield('num_50e_notes', 'Fifty euros: ', 'num_50e_notes', 3, 3, '0', false);
    output_submit_button('Compute');
    output_reset_button('CLEAR');
?>
    </form>
    
<?php
    output_footer('University College Cork');
?>

Libraries

Advantages of functions and libraries

Pointers for advanced students