An expression is a bit of PHP that can be evaluated to produce a value. The simplest expressions are literal values and variables. A literal value evaluates to itself, while a variable evaluates to the value stored in the variable. More complex expressions can be formed using simple expressions and operators.
An operator takes some values (the operands) and does something (for instance, adds them together). Operators are written as punctuation symbols—for instance, the + and - familiar to us from math. Some operators modify their operands, while most do not.
Table 2-3 summarizes the operators in PHP, many of which were borrowed from C and Perl. The column labeled "P" gives the operator's precedence; the operators are listed in precedence order, from highest to lowest. The column labeled "A" gives the operator's associativity, which can be L (left-to-right), R (right-to-left), or N (non-associative).
P |
A |
Operator |
Operation |
---|---|---|---|
19 |
N |
new |
Create new object |
18 |
R |
[ |
Array subscript |
17 |
R |
! |
Logical NOT |
R |
~ |
Bitwise NOT |
|
R |
++ |
Increment |
|
R |
-- |
Decrement |
|
R |
(int), (double), (string), (array), (object) |
Cast |
|
R |
@ |
Inhibit errors |
|
16 |
L |
* |
Multiplication |
L |
/ |
Division |
|
L |
% |
Modulus |
|
15 |
L |
+ |
Addition |
L |
- |
Subtraction |
|
L |
. |
String concatenation |
|
14 |
L |
<< |
Bitwise shift left |
L |
>> |
Bitwise shift right |
|
13 |
N |
<, <= |
Less than, less than or equal |
N |
>, >= |
Greater than, greater than or equal |
|
12 |
N |
== |
Value equality |
N |
!=, <> |
Inequality |
|
N |
=== |
Type and value equality |
|
N |
!== |
Type and value inequality |
|
11 |
L |
& |
Bitwise AND |
10 |
L |
^ |
Bitwise XOR |
9 |
L |
| |
Bitwise OR |
8 |
L |
&& |
Logical AND |
7 |
L |
|| |
Logical OR |
6 |
L |
?: |
Conditional operator |
5 |
L |
= |
Assignment |
L |
+=, -=, *=, /=, .=, %=, &=, |=, ^=, ~=, <<=, >>= |
Assignment with operation |
|
4 |
L |
and |
Logical AND |
3 |
L |
xor |
Logical XOR |
2 |
L |
or |
Logical OR |
1 |
L |
, |
List separator |
Most operators in PHP are binary operators; they combine two operands (or expressions) into a single, more complex expression. PHP also supports a number of unary operators, which convert a single expression into a more complex expression. Finally, PHP supports a single ternary operator that combines three expressions into a single expression.
The order in which operators in an expression are evaluated depends on their relative precedence. For example, you might write:
2 + 4 * 3
As you can see in Table 2-3, the addition and multiplication operators have different precedence, with multiplication higher than addition. So the multiplication happens before the addition, giving 2 + 12, or 14, as the answer. If the precedence of addition and multiplication were reversed, 6 * 3, or 18, would be the answer.
To force a particular order, you can group operands with the appropriate operator in parentheses. In our previous example, to get the value 18, you can use this expression:
(2 + 4) * 3
It is possible to write all complex expressions (expressions containing more than a single operator) simply by putting the operands and operators in the appropriate order so that their relative precedence yields the answer you want. Most programmers, however, write the operators in the order that they feel makes the most sense to programmers, and add parentheses to ensure it makes sense to PHP as well. Getting precedence wrong leads to code like:
$x + 2 / $y >= 4 ? $z : $x << $z
This code is hard to read and is almost definitely not doing what the programmer expected it to do.
One way many programmers deal with the complex precedence rules in programming languages is to reduce precedence down to two rules:
Multiplication and division have higher precedence than addition and subtraction.
Use parentheses for anything else.
Associativity defines the order in which operators with the same order of precedence are evaluated. For example, look at:
2 / 2 * 2
The division and multiplication operators have the same precedence, but the result of the expression depends on which operation we do first:
2/(2*2) // 0.5 (2/2)*2 // 2
The division and multiplication operators are left-associative; this means that in cases of ambiguity, the operators are evaluated from left to right. In this example, the correct result is 2.
Many operators have expectations of their operands—for instance, binary math operators typically require both operands to be of the same type. PHP's variables can store integers, floating-point numbers, strings, and more, and to keep as much of the type details away from the programmer as possible, PHP converts values from one type to another as necessary.
The conversion of a value from one type to another is called casting. This kind of implicit casting is called type juggling in PHP. The rules for the type juggling done by arithmetic operators are shown in Table 2-4.
Type of first operand |
Type of second operand |
Conversion performed |
---|---|---|
Integer |
Floating point |
The integer is converted to a floating-point number |
Integer |
String |
The string is converted to a number; if the value after conversion is a floating-point number, the integer is converted to a floating-point number |
Floating point |
String |
The string is converted to a floating-point number |
Some other operators have different expectations of their operands, and thus have different rules. For example, the string concatenation operator converts both operands to strings before concatenating them:
3 . 2.74 // gives the string 32.74
You can use a string anywhere PHP expects a number. The string is presumed to start with an integer or floating-point number. If no number is found at the start of the string, the numeric value of that string is 0. If the string contains a period (.) or upper- or lowercase e, evaluating it numerically produces a floating-point number. For example:
"9 Lives" - 1; // 8 (int) "3.14 Pies" * 2; // 6.28 (float) "9 Lives." - 1; // 8 (float) "1E3 Points of Light" + 1; // 1001 (float)
The arithmetic operators are operators you'll recognize from everyday use. Most of the arithmetic operators are binary; however, the arithmetic negation and arithmetic assertion operators are unary. These operators require numeric values, and non-numeric values are converted into numeric values by the rules described in Section 2.4.11. The arithmetic operators are:
Manipulating strings is such a core part of PHP applications that PHP has a separate string concatenation operator (.). The concatenation operator appends the righthand operand to the lefthand operand and returns the resulting string. Operands are first converted to strings, if necessary. For example:
$n = 5; $s = 'There were ' . $n . ' ducks.'; // $s is 'There were 5 ducks'
In programming, one of the most common operations is to increase or decrease the value of a variable by one. The unary autoincrement (++) and autodecrement (--) operators provide shortcuts for these common operations. These operators are unique in that they work only on variables; the operators change their operands' values as well as returning a value.
There are two ways to use autoincrement or autodecrement in expressions. If you put the operator in front of the operand, it returns the new value of the operand (incremented or decremented). If you put the operator after the operand, it returns the original value of the operand (before the increment or decrement). Table 2-5 lists the different operations.
Operator |
Name |
Value returned |
Effect on $var |
---|---|---|---|
$var++ |
Post-increment |
$var |
Incremented |
++$var |
Pre-increment |
$var + 1 |
Incremented |
$var-- |
Post-decrement |
$var |
Decremented |
--$var |
Pre-decrement |
$var - 1 |
Decremented |
These operators can be applied to strings as well as numbers. Incrementing an alphabetic character turns it into the next letter in the alphabet. As illustrated in Table 2-6, incrementing "z" or "Z" wraps it back to "a" or "Z" and increments the previous character by one, as though the characters were in a base-26 number system.
Incrementing this |
Gives this |
---|---|
"a" |
"b" |
"z" |
"aa" |
"spaz" |
"spba" |
"K9" |
"L0" |
"42" |
"43" |
As their name suggests, comparison operators compare operands. The result is always either true, if the comparison is truthful, or false, otherwise.
Operands to the comparison operators can be both numeric, both string, or one numeric and one string. The operators check for truthfulness in slightly different ways based on the types and values of the operands, either using strictly numeric comparisons or using lexicographic (textual) comparisons. Table 2-7 outlines when each type of check is used.
First operand |
Second operand |
Comparison |
---|---|---|
Number |
Number |
Numeric |
String that is entirely numeric |
String that is entirely numeric |
Numeric |
String that is entirely numeric |
Number |
Numeric |
String that is not entirely numeric |
Number |
Lexicographic |
String that is entirely numeric |
String that is not entirely numeric |
Lexicographic |
String that is not entirely numeric |
String that is not entirely numeric |
Lexicographic |
One important thing to note is that two numeric strings are compared as if they were numbers. If you have two strings that consist entirely of numeric characters and you need to compare them lexicographically, use the strcmp( ) function.
The comparison operators are:
The bitwise operators act on the binary representation of their operands. Each operand is first turned into a binary representation of the value, as described in the bitwise negation operator entry in the following list. All the bitwise operators work on numbers as well as strings, but they vary in their treatment of string operands of different lengths. The bitwise operators are:
111101101 & 110111001 --------- 110101001
The binary number 110101001 is octal 0651.[2] You can use the PHP functions bindec( ), decbin( ), octdec( ), and decoct( ) to convert numbers back and forth when you are trying to understand binary arithmetic.
[2]Here's a tip: split the binary number up into three groups. 6 is binary 110, 5 is binary 101, and 1 is binary 001; thus, 0651 is 110101001.
If both operands are strings, the operator returns a string in which each character is the result of a bitwise AND operation between the two corresponding characters in the operands. The resulting string is the length of the shorter of the two operands; trailing extra characters in the longer string are ignored. For example, "wolf" & "cat" is "cad".
If both operands are strings, the operator returns a string in which each character is the result of a bitwise OR operation between the two corresponding characters in the operands. The resulting string is the length of the longer of the two operands, and the shorter string is padded at the end with binary 0s. For example, "pussy" | "cat" is "suwsy".
If both operands are strings, this operator returns a string in which each character is the result of a bitwise XOR operation between the two corresponding characters in the operands. If the two strings are different lengths, the resulting string is the length of the shorter operand, and extra trailing characters in the longer string are ignored. For example, "big drink" ^ "AA" is "#(".
Note that each place to the left that a number is shifted results in a doubling of the number. The result of left shifting is multiplying the lefthand operand by 2 to the power of the righthand operand.
Logical operators provide ways for you to build complex logical expressions. Logical operators treat their operands as Boolean values and return a Boolean value. There are both punctuation and English versions of the operators (|| and or are the same operator). The logical operators are:
$result = $flag and mysql_connect( );
The && and and operators differ only in their precedence.
$result = fopen($filename) or exit( );
The || and or operators differ only in their precedence.
Although PHP is a weakly typed language, there are occasions when it's useful to consider a value as a specific type. The casting operators, (int) , (float), (string), (bool), (array), and (object), allow you to force a value into a particular type. To use a casting operator, put the operator to the left of the operand. Table 2-8 lists the casting operators, synonymous operands, and the type to which the operator changes the value.
Operator |
Synonymous operators |
Changes type to |
---|---|---|
(int) |
(integer) |
Integer |
(float) |
(real) |
Floating point |
(string) |
String |
|
(bool) |
(boolean) |
Boolean |
(array) |
Array |
|
(object) |
Object |
Casting affects the way other operators interpret a value, rather than changing the value in a variable. For example, the code:
$a = "5"; $b = (int) $a;
assigns $b the integer value of $a; $a remains the string "5". To cast the value of the variable itself, you must assign the result of a cast back into the variable:
$a = "5" $a = (int) $a; // now $a holds an integer
Not every cast is useful: casting an array to a numeric type gives 1, and casting an array to a string gives "Array" (seeing this in your output is a sure sign that you've printed a variable that contains an array).
Casting an object to an array builds an array of the properties, mapping property names to values:
class Person { var $name = "Fred"; var $age = 35; } $o = new Person; $a = (array) $o; print_r($a); Array ( [name] => Fred [age] => 35 )
You can cast an array to an object to build an object whose properties correspond to the array's keys and values. For example:
$a = array('name' => 'Fred', 'age' => 35, 'wife' => 'Wilma'); $o = (object) $a; echo $o->name; Fred
Keys that aren't valid identifiers, and thus are invalid property names, are inaccessible but are restored when the object is cast back to an array.
Assignment operators store or update values in variables. The autoincrement and autodecrement operators we saw earlier are highly specialized assignment operators—here we see the more general forms. The basic assignment operator is =, but we'll also see combinations of assignment and binary operations, such as += and &=.
The basic assignment operator (=) assigns a value to a variable. The lefthand operand is always a variable. The righthand operand can be any expression—any simple literal, variable, or complex expression. The righthand operand's value is stored in the variable named by the lefthand operand.
Because all operators are required to return a value, the assignment operator returns the value assigned to the variable. For example, the expression $a = 5 not only assigns 5 to $a, but also behaves as the value 5 if used in a larger expression. Consider the following expressions:
$a = 5; $b = 10; $c = ($a = $b);
The expression $a = $b is evaluated first, because of the parentheses. Now, both $a and $b have the same value, 10. Finally, $c is assigned the result of the expression $a = $b, which is the value assigned to the lefthand operand (in this case, $a). When the full expression is done evaluating, all three variables contain the same value, 10.
In addition to the basic assignment operator, there are several assignment operators that are convenient shorthand. These operators consist of a binary operator followed directly by an equals sign, and their effect is the same as performing the operation with the operands, then assigning the resulting value to the lefthand operand. These assignment operators are:
The remaining PHP operators are for error suppression, executing an external command, and selecting values:
$listing = `ls -ls /tmp`; echo $listing;
The conditional operator evaluates the expression before the ?. If the expression is true, the operator returns the value of the expression between the ? and :; otherwise, the operator returns the value of the expression after the :. For instance:
<a href="<?= $url ?>"><?= $linktext ? $linktext : $url ?></a>
If text for the link $url is present in the variable $linktext, it is used as the text for the link; otherwise, the URL itself is displayed.
Copyright © 2003 O'Reilly & Associates. All rights reserved.