start page | rating of books | rating of authors | reviews | copyrights

Programming PHPProgramming PHPSearch this book

Chapter 2. Language Basics

Contents:

Lexical Structure
Data Types
Variables
Expressions and Operators
Flow-Control Statements
Including Code
Embedding PHP in Web Pages

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.

2.1.1. Case Sensitivity

The names of user-defined classes and functions, as well as built-in constructs and keywords such as echo, while, class, etc., are case-insensitive. Thus, these three lines are equivalent:

echo("hello, world");
ECHO("hello, world");
EcHo("hello, world");

Variables, on the other hand, are case-sensitive. That is, $name, $NAME, and $NaME are three different variables.

2.1.2. Statements and Semicolons

A statement is a collection of PHP code that does something. It can be as simple as a variable assignment or as complicated as a loop with multiple exit points. Here is a small sample of PHP statements, including function calls, assignment, and an if test:

echo "Hello, world";
myfunc(42, "O'Reilly");
$a = 1;
$name = "Elphaba";
$b = $a / 25.0;
if ($a == $b) { echo "Rhyme? And Reason?"; }

PHP uses semicolons to separate simple statements. A compound statement that uses curly braces to mark a block of code, such as a conditional test or loop, does not need a semicolon after a closing brace. Unlike in other languages, in PHP the semicolon before the closing brace is not optional:

if ($needed) {
  echo "We must have it!";       // semicolon required here
}                                // no semicolon required here

The semicolon is optional before a closing PHP tag:

<?php
 if ($a == $b) { echo "Rhyme? And Reason?"; }
 echo "Hello, world"             // no semicolon required before closing tag
?>

It's good programming practice to include optional semicolons, as they make it easier to add code later.

2.1.3. Whitespace and Line Breaks

In general, whitespace doesn't matter in a PHP program. You can spread a statement across any number of lines, or lump a bunch of statements together on a single line. For example, this statement:

raise_prices($inventory, $inflation, $cost_of_living, $greed);

could just as well be written with more whitespace:

raise_prices (
                $inventory           ,
                $inflation           ,
                $cost_of_living      ,
                $greed
) ;

or with less whitespace:

raise_prices($inventory,$inflation,$cost_of_living,$greed);

You can take advantage of this flexible formatting to make your code more readable (by lining up assignments, indenting, etc.). Some lazy programmers take advantage of this free-form formatting and create completely unreadable code—this isn't recommended.

2.1.4. Comments

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.

2.1.4.3. 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:

<?php
 $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.5. Literals

A literal is a data value that appears directly in a program. The following are all literals in PHP:

2001
0xFE
1.4142
"Hello World"
'Hi'
true
null

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.

2.1.6.2. Function names

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

tally
list_all_users
deleteTclFiles
LOWERCASE_IS_FOR_WIMPS
_hide

These function names refer to the same function:

howdy  HoWdY  HOWDY  HOWdy  howdy

2.1.7. Keywords

A keyword is a word reserved by the language for its core functionality—you cannot give a variable, function, class, or constant the same name as a keyword. Table 2-1 lists the keywords in PHP, which are case-insensitive.

Table 2-1. PHP core language keywords

and
$argc
$argv
as
break
case
cfunction
class
continue
declare
default
die
do
E_ALL
echo
E_ERROR
else
elseif
empty
enddeclare
endfor
endforeach
endif
endswitch
E_PARSE
eval
E_WARNING
exit
extends
FALSE
for
foreach
function
$HTTP_COOKIE_VARS
$HTTP_ENV_VARS
$HTTP_GET_VARS
$HTTP_POST_FILES
$HTTP_POST_VARS
$HTTP_SERVER_VARS
if
include
include_once
global
list
new
not
NULL
old_function
or
parent
PHP_OS
$PHP_SELF
PHP_VERSION
print
require
require_once
return
static
stdClass
switch
$this
TRUE
var
virtual
while
xor
_  _FILE_  _
_  _LINE_  _
_  _sleep
_  _wakeup
$_COOKIE
$_ENV
$_FILES
$_GET
$_POST
$_SERVER

In addition, you cannot use an identifier that is the same as a built-in PHP function. For a complete list of these, see Appendix A.



Library Navigation Links

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