As you develop reusable code, you should consider how you will arrange your include files. By default, when a file is included using the include or require statements, PHP searches for the file in the same directory as the script being executed. You can include files in other directories by specifying a file path in the include or require statements. The following example shows how relative and absolute file paths can be used:
// a relative file path
The paths can be specified with forward slashes for both Unix and Microsoft Windows environments, allowing scripts to be moved from one environment to another. However, using paths can make it difficult to change the directory structure of your application.
A more sophisticated, and flexible alternative to accessing include files is to set theinclude_pathparameter defined in the php.ini configuration file. One or more directories can be specified in theinclude_pathparameter, and when set, PHP will search for include files relative to those directories. The following extract from the php.ini file shows how to set theinclude_pathparameter:
; UNIX: "/path1:/path2"
Path specifications for this parameter are system specific. Unix paths use the forward slash and are separated with the colon (:) character, while Microsoft Windows paths use the backslash and are separated by semi colons (;).
The php.ini configuration file defines many parameters that are used to define aspects of PHPís behavior. Whenever you change php.ini, you need to restart your Apache web server so that the changes are re-read; instructions for restarting are in Appendix A.
If you set theinclude_pathparameter,includeandrequiredirectives need only specify a path relative to a directory listed in theinclude_path. For example, if theinclude_pathis set to point at /usr/local/php/projectx, and you have an include file security.inc thatís stored in /usr/local/php/projectx/security, you only need to add:
to your script file. The PHP engine will check the directory /usr/local/php/projectx, and locate the subdirectory security and its include file. Include files that are placed in directories outside of the web serverís document root are protected from accessed via the web server. In Chapter 6 we describe how to protect include files that are under the web server root directory.
For a large project, you might place the project-specific code into one directory, while keeping reusable code in another; this is the approach we use in our case study, Hugh and Daveís Online Wines, as we describe in Chapter 15.A Working Example
In this section, we use some of the techniques described so far to develop a simple, complete PHP script. The script doesnít process input from the user, so we leave some of the best features of PHP as a web scripting language for discussion in later chapters.
Our example is a script that produces a web page containing the times tables. Our aim is to output the 1Ė12 times tables. The first table is shown in Figure 2-2 as rendered by a Mozilla browser.
Figure 2-2. The output of the times-tables script rendered in a Mozilla browser
The completed PHP script and HTML to produce the times tables are shown in Example 2-4. The first ten lines are the HTML markup that produces the<head>components and the<h1> The Times Tables</h1>heading at the top of the web page. Similarly, the last two lines are HTML that finishes the document:</body>and</html>.
Between the two HTML fragments that start and end the document is a PHP script to produce the times-table content and its associated HTML. The script begins with the PHP open tag<?phpand finishes with the close tag?>.
Example 2-4. A script to produce the times tables
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
The script is designed to process each times table and, for each table, to produce a heading and 12 lines. To do this, the script consists of two nested loops: an outer and innerforloop.
The outer for loop uses the integer variable$table, incrementing it by 1 each time the loop body is executed until$tableis greater than 12. The body of the outer loop prints the heading and executes the inner loop that actually produces the body of each times table.
The inner loop uses the integer variable$counterto generate the lines of the times tables. Inside the loop body, the$answerto the current line is calculated by multiplying the current value of$tableby the current value of$counter.
Every second line of the tables and the times-table headings are encapsulated in the bold tag<b>and bold end tag</b>, which produces alternating bold lines in the resulting HTML output. After calculating the$answer,anifstatement follows that decides whether the line should be output in bold tags. The expression theifstatement tests uses the modulo operator%to test if$counteris an odd or even number.
The modulo operation divides the variable$counterby 2 and returns the remainder. So, for example, if$counteris 6, the returned value is 0, because 6 divided by 2 is exactly 3 with no remainder. If$counteris 11, the returned value is 1, because 11 divided by 2 is 5 with a remainder of 1. If$counteris even, the conditional expression:
($counter % 2 == 0)
istrue, and bold tags are printed.
Example 2-4 is complete but not especially interesting. Regardless of how many times the script is executed, the result is the same web page. In practice, you might consider running the script once, capturing the output, and saving it to a static HTML file. If you save the output as HTML, the user can retrieve the same page, with less web-server load and a faster response time.
In later chapters, we develop scripts with output that can change from run to run, and canít be represented in a static file. In Chapter 6, we show scripts that interact with the MySQL database management system; the result is dynamic pages that change if the underlying data in the database is updated. We also show scripts that interact with the system environment and with user input from fill-in forms.