PHP
  Home arrow PHP arrow 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? 
Google.com  
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
    ( Page 1 of 10 )

    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).

    This chapter is the first of three that focus on the PHP scripting language. This chapter describes the PHP language basics. Chapter 3 describes PHP’s support for arrays, strings, and other data types, and Chapter 4 introduces object-oriented programming in PHP.

    If you’re familiar with any programming language, PHP should be easy to learn. If you have done no programming before, the pace of this chapter may be brisk but should still be manageable. PHP has a syntax similar to JavaScript, which many web designers have learned; both languages hark back to the classic C and Perl languages in syntax.

    The topics covered in this chapter include:

    1. PHP basics, including script structure, variables, supported types, constants, expressions, and type conversions
    2. Condition and branch statements supported by PHP, including if , if...else , and the switch statements
    3. Looping statements
    4. User-defined functions

    We conclude the chapter with a short example that puts many of the basic PHP concepts together.

    Introducing PHP

    The current version of PHP is PHP4 (Version 4.3.4). PHP5 is available for beta testing at the time of writing as Version 5.0.0b3. We discuss both versions in this chapter.

    PHP is a recursive acronym that stands for PHP: Hypertext Preprocessor; this is in the naming style of GNU, which stands for GNU’s Not Unix and which began this odd trend. The name isn’t a particularly good description of what PHP is and what it’s commonly used for. PHP is a scripting language that’s usually embedded or combined with the HTML of a web page. When the page is requested, the web server executes the PHP script and substitutes in the result back into the page. PHP has many excellent libraries that provide fast, customized access to DBMSs and is an ideal tool for developing application logic in the middle tier of a three-tier application.

    PHP Basics

    Example 2-1 shows the first PHP script in this book, the ubiquitous “Hello, world.” It’s actually mostly HTML; the PHP is embedded near the end.

    Example 2-1. The ubiquitous Hello, world in PHP

    <!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>Hello, world</title>
    </head>
    <body bgcolor="#ffffff">
     
    <h1>
     
    <?php
       
    print "Hello, world";
     
    ?>
     
    </h1>
    </body>
    </html>

    When requested by a web browser, the script is run on the web server and the resulting HTML document sent back to the browser and rendered as shown in Figure 2-1.

    Figure 2-1.  The output of Example 2-1 shown in the Netscape browser

    Example 2-1 illustrates the basic features of a PHP script. It’s a mixture of HTML—in this case it’s mostly HTML—and PHP code. The PHP code in this example:

    <?php
      
    print "Hello, world" ;
    ?>

    simply prints the greeting, “Hello, world.”

    The PHP script shown in Example 2-1 is rather pointless: we could simply have authored the HTML to include the greeting directly. Because PHP integrates so well with HTML, using PHP to produce static sequence of characters is far less compli cated and less interesting than using other high-level languages. However, the example does illustrate several features of PHP:

    1. A block of PHP code is embedded within HTML using the begin and end tags <?php and ?> . Other begin and end tag styles can also be used, such as the HTML style that is used with JavaScript or other embedded scripts: <script language="PHP"> and </script> . There’s also a shorter style <? and ?> . For consistency, we use only the <?php and ?> style in this book.
    2. Whitespace has no effect, except to aid readability for the developer. For example, the PHP could have been written succinctly as <?php print "Hello, world";?> with the same effect. Any mix of whitespace characters—spaces, tabs, carriage returns, and so on—can be used to separate PHP statements.
    3. A PHP script is a series of statements, each terminated with a semicolon. Our simple example has only one statement: print "Hello, world"; . PHP script can be anywhere in a file and interleaved with any HTML fragment. While Example 2-1 contains only one statement within one set of <?php and ?> tags, statements can be distribute code across multiple blocks of code.
    4. When PHP script is run, each block of code, including the start and end script tags <?php and ?> is replaced with the output of the block.

    When we present a few lines of code that are sections of larger scripts, we usually omit the start and end tags.

    The point of learning PHP, of course, is to create pages that change, pages that contain dynamic content derived from user input or a database. The first step toward that goal is to introduce a variable, which is something that can change from run to run. In this chapter, we don’t use dynamic content. But we can show how to set a variable to a string as follows:

    <?php $outputString = "Hello, world"; ?>

    And then rewrite our script as follows:

    <?php print $outputString; ?>

    Because $outputString has been set to Hello, world , that string is printed as part of the surrounding HTML page.

    The freedom to interleave blocks of PHP statements with HTML is one of the most powerful features of PHP. A short example is shown in Example 2-2; the variable $outputString is initialized before the start of the HTML document, and later this variable is output twice, as part of the <title> and <body> elements. We discuss more about variables and how to use them later in this chapter.

    Example 2-2. Embedding three blocks of code in a single document

    <?php $outputString = "Hello, world"; ? >
    <!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><?php print $outputString; ?></title>
    </head>
    <body bgcolor="#ffffff">
     
    <h1><?php print $outputString; ?></h1> </body>
    </html>

    The flexibility to add multiple blocks of PHP to HTML can also lead to unwieldy, hard-to-maintain code. Care should be taken in modularizing code and HTML; we discuss how to separate code and HTML using templates in Chapter 7.



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

       

    PHP ARTICLES

    - Getting Data from Yahoo Site Explorer Inboun...
    - Method Chaining: Adding More Selecting Metho...
    - How to Split a File During an FTP Upload Usi...
    - Expanding a Custom CodeIgniter Library with ...
    - Using the Yahoo Site Explorer Inbound Links ...
    - Building a CodeIgniter Custom Library with M...
    - Building an E-mini Trading System Using PHP ...
    - Completing the MySQL Class with Method Chain...
    - 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





    © 2003-2009 by Developer Shed. All rights reserved. DS Cluster 4 Hosted by Hostway
    For more Enterprise Application Development news, visit eWeek