Microsoft Word - Core PHP Programming Using PHP to Build Dynamic Web Sites

(singke) #1

Chapter 2. VARIABLES, OPERATORS, AND


EXPRESSIONS


Everything in PHP is either an identifier or an operator. An identifier can be a function or
variable. An operator is usually one or two symbols that stand for some sort of data
manipulation like addition or multiplication. When identifiers and operators are
combined, they become an expression. This chapter introduces the concepts that form the
basis of all PHP code.


Identifiers


Identifiers give names to the abstract parts of PHP: functions, variables, and classes.
Some of them are created by PHP in the form of built-in functions or environment
variables. Others you create. Identifiers may be of any length and can consists of letters,
numbers, or underscores. The first character of an identifier must be either a letter or an
underscore. Table 2.1 contrasts acceptable identifiers with unacceptable ones.


Upper- and lowercase letters are recognized as different. That is, the variables UserName
and username are two distinct identifiers. The exception is built-in functions. As stated in
Chapter 1, "An Introduction to PHP," functions like print can be called as Print if you
prefer.


Table 2.1. Acceptable and Unacceptable Identifiers
Acceptable Unacceptable
LastVisit Last!Visit
_password ~password
compute_Mean compute-Mean
Lucky7 7Lucky


Variables, discussed in detail below, are always preceded by $. The side effect of this is
that a function and a variable can share a name. You may also create a variable with the
same name as a built-in function. Consider that this can be very confusing to anyone
reading your code, including you. You may never create a function with the same name
as a built-in function.


Data Types


PHP has three elemental types of data: integers, floating-point numbers, and strings of
text. Integers are sometimes referred to as whole or natural numbers; they contain no
decimal point. Floating-point numbers are sometimes called real numbers. They always
contain a decimal point, even when only a zero follows it. PHP refers to these as doubles,
which is short for double-precision floating-point numbers. Strings are collections of

Free download pdf