Home arrow PHP arrow The PHP Scripting Language

The PHP Scripting Language

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



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, includingif,if...else, and theswitchstatements
  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>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
<title>Hello, world</title>
<body bgcolor="#ffffff">
print "Hello, world";

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:

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 complicated 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<?phpand?>. 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<?phpand?>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<?phpand?>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<?phpand?>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$outputStringhas been set toHello, 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$outputStringis 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"
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
<title><?php print $outputString; ?></title>
<body bgcolor="#ffffff">
<h1><?php print $outputString; ?></h1> </body>

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

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: