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.
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.
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.
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, some variable data assignments, and an if
statement:
echo "Hello, world"; myFunction(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 after the brace
The semicolon, however, 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.
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:
raisePrices($inventory, $inflation, $costOfLiving, $greed);
could just as well be written with more whitespace:
raisePrices ( $inventory , $inflation , $costOfLiving , $greed ) ;
or with less whitespace:
raisePrices($inventory,$inflation,$costOfLiving,$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 freeform formatting and create completely unreadable code—this is not recommended.
Comments give information to people who read your code, but they are ignored by PHP at execution time. 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 could easily look as though a stranger wrote it.
A good practice is to make your comments sparse enough not to get in the way of the code itself but 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 the comments on this complex regular expression will help whoever maintains your code:
// convert &#nnn; entities into characters $text = preg_replace('/&#([0-9])+;/', "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.
When PHP encounters a hash mark character (#
) within the code, everything from the hash mark to the end of the line or the end of the section of PHP code (whichever comes first) is considered a comment. This method of commenting is found in Unix shell scripting languages and is useful for annotating single lines of code or making short notes.
Because the hash mark is visible on the page, shell-style comments are sometimes used to mark off blocks of code:
####################### ## Cookie functions #######################
Sometimes they’re used before a line of code to identify what that code does, in which case they’re usually indented to the same level as the code for which the comment is intended:
if ($doubleCheck) { # create an HTML form requesting that the user confirm the action echo confirmationForm(); }
Short comments on a single line of code are often put on the same line as the code:
$value = $p * exp($r * $t); # calculate compounded interest
When you’re tightly mixing HTML and PHP code, it can be useful to have the closing PHP tag terminate the comment:
<?php $d = 4; # Set $d to 4. ?> Then another <?php echo $d; ?> Then another 4
When PHP encounters two slashes (//
) within the code, everything from the slashes to the end of the line or the end of the section of code, whichever comes first, is considered a comment. This method of commenting is derived from C++. The result is the same as the shell comment style.
Here are the shell-style comment examples, rewritten to use C++ comments:
//////////////////////// // Cookie functions //////////////////////// if ($doubleCheck) { // create an HTML form requesting that the user confirm the action echo confirmationForm(); } $value = $p * exp($r * $t); // calculate compounded interest <?php $d = 4; // Set $d to 4. ?> Then another <?php echo $d; ?> Then another 4
While shell-style and C++-style comments are useful for annotating code or making short notes, longer comments require a different style. Therefore, 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 and is discouraged:
/* These comments can be mixed with code too, see? */ $e = 5; /* This works just fine. */
C-style comments, unlike the other types, can continue past the end PHP tag 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 "); ?><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 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, as well as the inline comment, 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.
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
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 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.
Variable names always begin with a dollar sign ($
) and are case-sensitive. Here are some valid variable names:
$bill $head_count $MaximumForce $I_HEART_PHP $_underscore $_int
Here are some illegal variable names:
$not valid $| $3wa
These variables are all different due to case sensitivity:
$hot_stuff $Hot_stuff $hot_Stuff $HOT_STUFF
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 all refer to the same function:
howdy HoWdY HOWDY HOWdy howdy
Class names follow the standard rules for PHP identifiers and are also not case-sensitive. Here are some valid class names:
Person account
The class name stdClass
is a reserved class name.
A constant is an identifier for a value that will not be changed; scalar values (Boolean, integer, double, and string) and arrays can be constants. Once set, the value of a constant cannot change. Constants are referred to by their identifiers and are set using the define()
function:
define('PUBLISHER', "O'Reilly Media"); echo PUBLISHER;
A keyword (or reserved word) is a word set aside by the language for its core functionality—you cannot give a function, class, or constant the same name as a keyword. Table 2-1 lists the keywords in PHP, which are case-insensitive.
__CLASS__ __DIR__ __FILE__ __FUNCTION__ __LINE__ __METHOD__ __NAMESPACE__ __TRAIT__ __halt_compiler() abstract and array() as break callable case catch class clone const continue declare default die() do |
echo else elseif empty() enddeclare endfor endforeach endif endswitch endwhile eval() exit() extends final finally for foreach function global goto if implements include include_once instanceof |
insteadof interface isset() list() namespace new or print private protected public require require_once return static switch throw trait try unset() use var while xor yield yield from |
In addition, you cannot use an identifier that is the same as a built-in PHP function. For a complete list of these, see the appendix.
PHP provides eight types of values, or data types. Four are scalar (single-value) types: integers, floating-point numbers, strings, and Booleans. Two are compound (collection) types: arrays and objects. The remaining two are special types: resource and NULL. Numbers, Booleans, resources, and NULL are discussed in full here, while strings, arrays, and objects are big enough topics that they get their own chapters (Chapter 4, Chapter 5, and Chapter 6, respectively).
Integers are whole numbers, such as 1, 12, and 256. The range of acceptable values varies according to the details of your platform but typically extends from −2,147,483,648 to +2,147,483,647. Specifically, the range is equivalent to the range of the long data type of your C compiler. Unfortunately, the C standard doesn’t specify what range that long type should have, so on some systems you might see a different integer range.
Integer literals can be written in decimal, octal, binary, or hexadecimal. Decimal values are represented by a sequence of digits, without leading zeros. The sequence may begin with a plus (+
) or minus (−
) sign. If there is no sign, positive is assumed. Examples of decimal integers include the following:
1998 −641 +33
Octal numbers consist of a leading 0
and a sequence of digits from 0 to 7. Like decimal numbers, octal numbers can be prefixed with a plus or minus. Here are some example octal values and their equivalent decimal values:
0755 // decimal 493 +010 // decimal 8
Hexadecimal values begin with 0x
, followed by a sequence of digits (0–9) or letters (A–F). The letters can be upper- or lowercase but are usually written in capitals. As with decimal and octal values, you can include a sign in hexadecimal numbers:
0xFF // decimal 255 0x10 // decimal 16 -0xDAD1 // decimal −56017
Binary numbers begin with 0b
, followed by a sequence of digits (0 and 1). As with other values, you can include a sign in binary numbers:
0b01100000 // decimal 96 0b00000010 // decimal 2 -0b10 // decimal −2
If you try to store a variable that is too large to be stored as an integer or is not a whole number, it will automatically be turned into a floating-point number.
Use the is_int()
function (or its is_integer()
alias) to test whether a value is an integer:
if (is_int($x)) { // $x is an integer }
Floating-point numbers (often referred to as “real” numbers) represent numeric values with decimal digits. Like integers, their limits depend on your machine’s details. PHP floating-point numbers are equivalent to the range of the double data type of your C compiler. Usually, this allows numbers between 1.7E−308 and 1.7E+308 with 15 digits of accuracy. If you need more accuracy or a wider range of integer values, you can use the BC or GMP extensions.
PHP recognizes floating-point numbers written in two different formats. There’s the one we all use every day:
3.14 0.017 -7.1
but PHP also recognizes numbers in scientific notation:
0.314E1 // 0.314*10^1, or 3.14 17.0E-3 // 17.0*10^(-3), or 0.017
Floating-point values are only approximate representations of numbers. For example, on many systems 3.5 is actually represented as 3.4999999999. This means you must take care to avoid writing code that assumes floating-point numbers are represented completely accurately, such as directly comparing two floating-point values using ==
. The normal approach is to compare to several decimal places:
if (intval($a * 1000) == intval($b * 1000)) { // numbers equal to three decimal places }
Use the is_float()
function (or its is_real()
alias) to test whether a value is a floating-point number:
if (is_float($x)) { // $x is a floating-point number }
Because strings are so common in web applications, PHP includes core-level support for creating and manipulating strings. A string is a sequence of characters of arbitrary length. String literals are delimited by either single or double quotes:
'big dog' "fat hog"
Variables are expanded (interpolated) within double quotes, while within single quotes they are not:
$name = "Guido"; echo "Hi, $name <br/>"; echo 'Hi, $name'; Hi, Guido Hi, $name
Double quotes also support a variety of string escapes, as listed in Table 2-2.
Escape sequence | Character represented |
---|---|
" |
Double quotes |
|
Newline |
|
Carriage return |
|
Tab |
\ |
Backslash |
$ |
Dollar sign |
{ |
Left brace |
} |
Right brace |
[ |
Left bracket |
] |
Right bracket |
|