So far, you've learned the basic structure of a user-defined function, but not how it fits within your own scripts. In the case of the multiplier() function, it does seem awfully time-consuming to create a script like the following just to print a number on the screen:
Instead, imagine a function called db_connect(), which contains your database connection and selection code:
function db_connect();
$connection = @mysql_connect("localhost", "spike", "9sj7En4")
or die(mysql_error());
$db = @mysql_select_db($db_name, $connection)
or die(mysql_error());
}
Instead of typing those two lines over and over in every script, imagine simply typing this:
db_connect();
If your host name, username, password, or database name change, you have to change this information in only one place—the db_connect() function code. Now the only trick is where to put this function. Obviously, if you are creating a function in order to reuse the code within it, you don't want the function to be part of the script. Instead, you place the function in a file of its own (or a file containing other functions) and use include() or require() to pull the information into your script as appropriate.
The include() and require() functions do essentially the same thing: When called, the code in the included file becomes part of the script calling it. From that point forward, anything in the included file can be used in the script calling it.
The difference between include() and require() pops up when the file to be included cannot be opened. This can occur because of incorrect permissions, or perhaps the file isn't in the location specified. When a failure occurs using include(), you get a warning, but the code continues to execute as best it can, which is to say, not very well if it needs a function that's in some other file! When require() cannot find or open the file, you will get a fatal error and PHP will stop processing the code altogether.
Included files look just like any other PHP files, starting with an opening PHP tag and ending with a closing PHP tag. For example, suppose you have a file called myfunctions.php, containing the following code:
<?
/* The multiplier() function multiplies a number by 5 and returns it */
function multiplier($num) {
$answer = $num * 5;
return $answer;
}
/* The db_connect() function connects to my database. */
function db_connect() {
$connection = @mysql_connect("localhost", "spike", "9sj7En4")
or die(mysql_error());
$db = @mysql_select_db($db_name, $connection)
or die(mysql_error());
}
?>
Then, in your actual PHP script, which needs to connect to a database, you would have the following:
<?
//include the file that has the function you need
include("/path/to/myfunctions.php");
//call a function
db_connect();
//now you can do things like issue queries and get results
?>
Obviously, this is a very simple example of using included files as function libraries, but you probably can already see the benefits! Anywhere you have repetitive code, think about using your own function to replace it. You can have multiple files full of function libraries named appropriately for their tasks. You can easily optimize your code when you go through the exercise of determining where functions can be used. Try it yourself—there is plenty of repetitive code used in this book, which you can quickly turn into your own tightly wound application!