home | O'Reilly's CD bookshelfs | FreeBSD | Linux | Cisco | Cisco Exam  

Programming PHPProgramming PHPSearch this book

Chapter 2. Language Basics

This chapter provides a whirlwind tour of the core PHP language, covering such basic topics as data types, variables, operators, and flow control statements. PHP is strongly influenced by other programming languages, such as Perl and C, so if you've had experience with those languages, PHP should be easy to pick up. If PHP is one of your first programming languages, don't panic. We start with the basic units of a PHP program and build up your knowledge from there.

2.1. Lexical Structure

The lexical structure of a programming language is the set of basic rules that governs how you write programs in that language. It is the lowest-level syntax of the language and specifies such things as what variable names look like, what characters are used for comments, and how program statements are separated from each other.


Comments give information to people who read your code, but they are ignored by PHP. Even if you think you're the only person who will ever read your code, it's a good idea to include comments in your code—in retrospect, code you wrote months ago can easily look as though a stranger wrote it.

Good practice is to make your comments sparse enough not to get in the way of the code itself and plentiful enough that you can use the comments to tell what's happening. Don't comment obvious things, lest you bury the comments that describe tricky things. For example, this is worthless:

$x = 17;    // store 17 into the variable $x

whereas this may well help whoever will maintain your code:

// convert &#nnn; entities into characters
$text = preg_replace('/&#([0-9])+);/e', "chr('\\1')", $text);

PHP provides several ways to include comments within your code, all of which are borrowed from existing languages such as C, C++, and the Unix shell. In general, use C-style comments to comment out code, and C++-style comments to comment on code. C comments

While shell- and C++-style comments are useful for annotating code or making short notes, longer comments require a different style. As such, PHP supports block comments, whose syntax comes from the C programming language. When PHP encounters a slash followed by an asterisk (/*), everything after that until it encounters an asterisk followed by a slash (*/) is considered a comment. This kind of comment, unlike those shown earlier, can span multiple lines.

Here's an example of a C-style multiline comment:

/* In this section, we take a bunch of variables and
   assign numbers to them. There is no real reason to
   do this, we're just having fun.
  $a = 1; $b = 2; $c = 3; $d = 4;

Because C-style comments have specific start and end markers, you can tightly integrate them with code. This tends to make your code harder to read, though, so it is frowned upon:

/* These comments can be mixed with code too,
see? */ $e = 5; /* This works just fine. */

C-style comments, unlike the other types, continue past end markers. For example:

 $l = 12;
 $m = 13;
/* A comment begins here
<p>Some stuff you want to be HTML.</p>
<?= $n = 14; ?>
  echo("l=$l m=$m n=$n\n");
<p>Now <b>this</b> is regular HTML...</p>
l=12 m=13 n=
<p>Now <b>this</b> is regular HTML...</p>

You can indent, or not indent, comments as you like:

/* There are no
special indenting or spacing
      rules that have to be followed, either.


C-style comments can be useful for disabling sections of code. In the following example, we've disabled the second and third statements by including them in a block comment. To enable the code, all we have to do is remove the comment markers:

    $f = 6;
/*  $g = 7;   # This is a different style of comment
    $h = 8;

However, you have to be careful not to attempt to nest block comments:

    $i = 9;
/*  $j = 10; /* This is a comment */
    $k = 11;
Here is some comment text.

In this case, PHP tries (and fails) to execute the (non-)statement Here is some comment text and returns an error.

2.1.6. Identifiers

An identifier is simply a name. In PHP, identifiers are used to name variables, functions, constants, and classes. The first character of an identifier must be either an ASCII letter (uppercase or lowercase), the underscore character (_), or any of the characters between ASCII 0x7F and ASCII 0xFF. After the initial character, these characters and the digits 0-9 are valid. Function names

Function names are not case-sensitive (functions are discussed in more detail in Chapter 3). Here are some valid function names:


These function names refer to the same function:

howdy  HoWdY  HOWDY  HOWdy  howdy

Library Navigation Links

Copyright © 2003 O'Reilly & Associates. All rights reserved.