PHP
  Home arrow PHP arrow Page 10 - The PHP Scripting Language
Dev Shed Forums  
Administration  
AJAX  
Apache  
BrainDump  
DHTML  
Flash  
Java  
JavaScript  
Multimedia  
MySQL  
Oracle  
Perl  
PHP  
Practices  
Python  
Reviews  
Security  
Smartphone Development  
Style-Sheets  
Web Services  
XML  
Zend  
Zope  
Mobile Linux  
App Generation ROI  
IBM® developerWorks  
Forums Sitemap  
E-Commerce Hosting  
Linux Web Hosting  
Managed Hosting  
Small Business Hosting  
VPS Hosting  
Weekly Newsletter

 
Developer Updates  
Free Website Content 
 RSS  Articles
 RSS  Forums
 RSS  All Feeds
Write For Us Get Paid  
Request Media Kit
Contact Us  
Site Map  
Privacy Policy  
Support  
 USERNAME
 
 PASSWORD
 
 
  >>> SIGN UP!  
  Lost Password? 
PHP

The PHP Scripting Language
By: O'Reilly Media
  • Search For More Articles!
  • Disclaimer
  • Author Terms
  • Rating: starstarstarstarstar / 31
    2005-09-29


    Table of Contents:
  • The PHP Scripting Language
  • Creating PHP scripts
  • Character encoding
  • Expressions, Operators, and Variable Assignment
  • switch Statement
  • Changing Loop Behavior
  • Automatic Type Conversion
  • User-Defined Functions
  • Static variables
  • Managing include files

  • Rate this Article: Poor Best 
      ADD THIS ARTICLE TO:
      error-file:tidyout.log Del.ici.ous error-file:tidyout.log Digg
      error-file:tidyout.log Blink error-file:tidyout.log Simpy
      error-file:tidyout.log Google error-file:tidyout.log Spurl
      error-file:tidyout.log Y! MyWeb error-file:tidyout.log Furl
    Email Me Similar Content When Posted
    Add Developer Shed Article Feed To Your Site
    Email Article To Friend
    Print Version Of Article
    PDF Version Of Article

     
     
    ADVERTISEMENT


    The PHP Scripting Language - Managing include files
    ( Page 10 of 10 )

    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 pat h
    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 Win dows 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 the include_path parameter defined in the php.ini configuration file. One or more directories can be specified in the include_path parameter, 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 the include_path parameter:

    ;;;;;;;;;;;;;;;;;;;;;;;;;
    ; 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 for ward 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 the include_path parameter, include and require directives need only specify a path relative to a directory listed in the include_path . For example, if the include_path is 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 <?php and 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>
    <head>
     
    <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
     
    <title>The Times-Tables</title>
    </head>
    <body bgcolor="#ffffff">
    <h1>The Times Tables</h1>
    <?php
    // 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 = " .
                  
    "$answer</b>";
           
    else
             
    // No, so print this in normal face
             
    print "<br>$counter x $table = $answer";
     
    }
    }
    ?>
    </body>
    </html>

    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 inner for loop.

    The outer for  loop uses the integer variable $table , incrementing it by 1 each time the loop body is executed until $table is 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 $counter to generate the lines of the times tables. Inside the loop body, the $answer to the current line is calculated by multiplying the current value of $table by 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 ,an if statement follows that decides whether the line should be output in bold tags. The expression the if statement tests uses the modulo operator % to test if $counter is an odd or even number.

    The modulo operation divides the variable $counter by 2 and returns the remainder. So, for example, if $counter is 6, the returned value is 0, because 6 divided by 2 is exactly 3 with no remainder. If $counter is 11, the returned value is 1, because 11 divided by 2 is 5 with a remainder of 1. If $counter is even, the conditional expression:

    ($counter % 2 == 0)

    is true , 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
     

       

    PHP ARTICLES

    - Building Dynamic Queries with Chainable Meth...
    - PHP Encryption and Decryption Methods
    - Building a MySQL Abstraction Class with Meth...
    - Completing a Sample String Processor with Me...
    - Mastering WHILE Loops for PHP and MySQL
    - Method Chaining: Adding More Methods to the ...
    - Method Chaining in PHP 5
    - The Role of Interfaces in Applying the Depen...
    - Dependency Injection: Using a Setter Method ...
    - Using a Model Class with the Dependency Inje...
    - Injecting Objects Using Setter Methods with ...
    - Injecting Objects by Constructor with the De...
    - The Dependency Injection Design Pattern in P...
    - Performing Inferential Statistical Analysis ...
    - Performing Descriptive Statistical Analysis ...





    © 2003-2009 by Developer Shed. All rights reserved. DS Cluster 6 Hosted by Hostway
    Stay green...Green IT