Home arrow PHP arrow Page 10 - The PHP Scripting Language

Managing include files - PHP

This article describes the basics of the PHP scripting language, which is very easy to learn if you are familiar with any programming language. It is excerpted from chapter two of the book Web Database Applications with PHP and MySQL, written by Hugh E. Williams and David Lane (O'Reilly, 2004; ISBN: 0596005431).

  1. The PHP Scripting Language
  2. Creating PHP scripts
  3. Character encoding
  4. Expressions, Operators, and Variable Assignment
  5. switch Statement
  6. Changing Loop Behavior
  7. Automatic Type Conversion
  8. User-Defined Functions
  9. Static variables
  10. Managing include files
By: O'Reilly Media
Rating: starstarstarstarstar / 32
September 29, 2005

print this article



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
require "../inc/myFunctions.php";
// an absolute file path
require "/library/database/db.inc";

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:

; Paths and Directories ;

; UNIX: "/path1:/path2"
;include_path = ".:/php/includes:/usr/local/php/projectx"
; Windows: "\path1;\path2"
include_path = ".;c:\php\includes;d:\php\projectx"

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:

include "security/security.inc";

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"
http://www.w3.org/TR/html401/loose.dtd> <html>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
<title>The Times-Tables</title>
<body bgcolor="#ffffff">
<h1>The Times Tables</h1>
// Go through each table
for($table=1; $table<13; $table++)
print "<p><b>The " . $table . " Times Table</b>\n";
// Produce 12 lines for each table
for($counter=1; $counter<13; $counter++)
$answer = $table * $counter;
// Is this an even-number counter?
if ($counter % 2 == 0)
// Yes, so print this line in bold
print "<br><b>$counter x $table = " .
// No, so print this in normal face
print "<br>$counter x $table = $answer";

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.

>>> More PHP Articles          >>> More By O'Reilly Media

blog comments powered by Disqus
escort Bursa Bursa escort Antalya eskort


- Hackers Compromise PHP Sites to Launch Attac...
- Red Hat, Zend Form OpenShift PaaS Alliance
- PHP IDE News
- BCD, Zend Extend PHP Partnership
- PHP FAQ Highlight
- PHP Creator Didn't Set Out to Create a Langu...
- PHP Trends Revealed in Zend Study
- PHP: Best Methods for Running Scheduled Jobs
- PHP Array Functions: array_change_key_case
- PHP array_combine Function
- PHP array_chunk Function
- PHP Closures as View Helpers: Lazy-Loading F...
- Using PHP Closures as View Helpers
- PHP File and Operating System Program Execut...
- PHP: Effects of Wrapping Code in Class Const...

Developer Shed Affiliates


Dev Shed Tutorial Topics: