Online Test Banks
Score higher
See Online Test Banks
eLearning
Learning anything is easy
Browse Online Courses
Mobile Apps
Learning on the go
Explore Mobile Apps
Dummies Store
Shop for books and more
Start Shopping

Reuse Code in PHP Programs through Functions

You can, with the help of an auto_prepend_file, create a functions file that's automatically included within all your PHP programs. These functions might be something as simple as starting a session or as complex as an entire login function.

Whenever you need or think you need to have a function in more than one file, rather than using require_once and include_once, if you're going to use a function in multiple places then you can just as easily place it in an auto_prepend file.

Here's an example of how you can reuse code through functions. One function that you might use in many places is something to convert a two-letter state abbreviation to its full name. You can create a function to do so and place it in the prepended PHP file.

This exercise assumes that you've completed the preceding exercise to create a prepend.php file and have that file automatically loading through your web server.

  1. Open prepend.php from the preceding exercise.

  2. Clear any code out of prepend.php and place the following code in the file:

<?php
 
if (!isset($_SESSION)) {
        session_start();
}
 
function convertState($state) {
        $stateList = array(
                "AL" => "Alabama",
                "AK" => "Alaska",
                "AZ" => "Arizona",
                "AR" => "Arkansas",
                "CA" => "California",
                "CO" => "Colorado",
                "CT" => "Connecticut",
                "DE" => "Delaware",
                "FL" => "Florida",
                "GA" => "Georgia",
                "HI" => "Hawaii",
                "ID" => "Idaho",
                "IL" => "Illinois",
                "IN" => "Indiana",
                "IA" => "Iowa",
                "KS" => "Kansas",
                "KY" => "Kentucky",
                "LA" => "Louisiana",
                "ME" => "Maine",
                "MD" => "Maryland",
                "MA" => "Massachusetts",
                "MI" => "Michigan",
                "MN" => "Minnesota",
                "MS" => "Mississippi",
                "MO" => "Missouri",
                "MT" => "Montana",
                "NE" => "Nebraska",
                "NV" => "Nevada",
                "NH" => "New Hampshire",
                "NJ" => "New Jersey",
                "NM" => "New Mexico",
                "NY" => "New York",
                "NC" => "North Carolina",
                "ND" => "North Dakota",
                "OH" => "Ohio",
                "OK" => "Oklahoma",
                "OR" => "Oregon",
                "PA" => "Pennsylvania",
                "RI" => "Rhode Island",
                "SC" => "South Carolina",
                "SD" => "South Dakota",
                "TN" => "Tennessee",
                "TX" => "Texas",
                "UT" => "Utah",
                "VT" => "Vermont",
                "VA" => "Virginia",
                "WA" => "Washington",
                "WV" => "West Virginia",
                "WI" => "Wisconsin",
                "WY" => "Wyoming"
        );
        if (array_key_exists($state,$stateList)) {
                return $stateList[$state];
        } else {
                return false;
        }
} //end function convertState   
 
?>

Save the file (as prepend.php) in your document root.

Create a new file in your text editor and place the following code into the editor:

<?php
 
$stateAbbrev = "WI";
 
print "State abbreviation is " . $stateAbbrev . "<br>\n";
 
$stateFull = convertState($stateAbbrev);
 
if ($stateFull) {
        print "Full name is " . $stateFull . "<br>\n";
} else {
        print "Full name not found for {$stateAbbrev}<br>\n";
}
 
 ?>

Save the file as state.php in your document root. Open a browser and point to http://localhost/state.php. You should see a page like this one.

image0.jpg

The code in the prepend.php file first checks to see if the session has been started and starts the session, if necessary. Though it isn't used in this file, it'll be used elsewhere. The function, called convertState, accepts an argument of the state to convert.

The function sets up an array of the states and their full names. After that, the array_key_exists() PHP function is used to look up the state. If the two-letter abbreviation doesn't exist in the array, false is returned. Otherwise the name of the state is returned.

The state.php file merely called the convertState function, which is automatically "visible" or available because of the auto_prepend_file directive that you already set up. If there's a value in the $stateFull variable, then it's printed; otherwise, if there's no value, as it would be if the value was set to Boolean false (like it might be if no state was found), then a note is printed to that effect.

This example demonstrates a simple but typical function that might be commonly used across a web application built with PHP. By moving this function into a file that's included everywhere, you can use the function without having to do any extra work, like requiring or including the function's file, wherever you want the function's result.

  • Add a Comment
  • Print
  • Share
blog comments powered by Disqus
Advertisement

Inside Dummies.com

Dummies.com Sweepstakes

Win $500. Easy.