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

Programming PHPProgramming PHPSearch this book

2.4. Expressions and Operators

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).

Table 2-3. PHP operators

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

2.4.2. Operator Precedence

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.

2.4.4. Implicit Casting

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.

Table 2-4. Implicit casting rules for binary arithmetic operations

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)

2.4.5. Arithmetic Operators

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:

Addition (+)
The result of the addition operator is the sum of the two operands.

Subtraction (-)
The result of the subtraction operator is the difference between the two operands; i.e., the value of the second operand subtracted from the first.

Multiplication (*)
The result of the multiplication operator is the product of the two operands. For example, 3 * 4 is 12.

Division (/)
The result of the division operator is the quotient of the two operands. Dividing two integers can give an integer (e.g., 4/2) or a floating-point result (e.g., 1/2).

Modulus (%)
The modulus operator converts both operands to integers and returns the remainder of the division of the first operand by the second operand. For example, 10 % 6 is 4.

Arithmetic negation (-)
The arithmetic negation operator returns the operand multiplied by -1, effectively changing its sign. For example, -(3 - 4) evaluates to 1. Arithmetic negation is different from the subtraction operator, even though they both are written as a minus sign. Arithmetic negation is always unary and before the operand. Subtraction is binary and between its operands.

Arithmetic assertion (+)
The arithmetic assertion operator returns the operand multiplied by +1, which has no effect. It is used only as a visual cue to indicate the sign of a value. For example, +(3 - 4) evaluates to -1, just as (3 - 4) does.

2.4.8. Comparison Operators

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.

Table 2-7. Type of comparision performed by the comparision operators

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:

Equality (==)
If both operands are equal, this operator returns true; otherwise, it returns false.

Identical (===)
If both operands are equal and are of the same type, this operator returns true; otherwise, it returns false. Note that this operator does not do implicit type casting. This operator is useful when you don't know if the values you're comparing are of the same type. Simple comparison may involve value conversion. For instance, the strings "0.0" and "0" are not equal. The == operator says they are, but === says they are not.

Inequality (!= or <>)
If both operands are not equal, this operator returns true; otherwise, it returns false.

Not identical (!==)
If both operands are not equal, or they are not of the same type, this operator returns true; otherwise, it returns false.

Greater than (>)
If the lefthand operator is greater than the righthand operator, this operator returns true; otherwise, it returns false.

Greater than or equal to (>=)
If the lefthand operator is greater than or equal to the righthand operator, this operator returns true; otherwise, it returns false.

Less than (<)
If the lefthand operator is less than the righthand operator, this operator returns true; otherwise, it returns false.

Less than or equal to (<=)
If the lefthand operator is less than or equal to the righthand operator, this operator returns true; otherwise, it returns false.

2.4.9. Bitwise Operators

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:

Bitwise negation (~)
The bitwise negation operator changes 1s to 0s and 0s to 1s in the binary representations of the operands. Floating-point values are converted to integers before the operation takes place. If the operand is a string, the resulting value is a string the same length as the original, with each character in the string negated.

Bitwise AND (&)
The bitwise AND operator compares each corresponding bit in the binary representations of the operands. If both bits are 1, the corresponding bit in the result is 1; otherwise, the corresponding bit is 0. For example, 0755 & 0671 is 0651. This is a bit easier to understand if we look at the binary representation. Octal 0755 is binary 111101101, and octal 0671 is binary 110111001. We can the easily see which bits are on in both numbers and visually come up with the answer:

  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".

Bitwise OR (|)
The bitwise OR operator compares each corresponding bit in the binary representations of the operands. If both bits are 0, the resulting bit is 0; otherwise, the resulting bit is 1. For example, 0755 | 020 is 0775.

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".

Bitwise XOR (^)
The bitwise XOR operator compares each corresponding bit in the binary representation of the operands. If either of the bits in the pair, but not both, is 1, the resulting bit is 1; otherwise, the resulting bit is 0. For example, 0755 ^ 023 is 776.

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 "#(".

Left shift (<<)
The left shift operator shifts the bits in the binary representation of the lefthand operand left by the number of places given in the righthand operand. Both operands will be converted to integers if they aren't already. Shifting a binary number to the left inserts a 0 as the rightmost bit of the number and moves all other bits to the left one place. For example, 3 << 1 (or binary 11 shifted one place left) results in 6 (binary 110).

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.

Right shift (>>)
The right shift operator shifts the bits in the binary representation of the lefthand operand right by the number of places given in the righthand operand. Both operands will be converted to integers if they aren't already. Shifting a binary number to the right inserts a 0 as the leftmost bit of the number and moves all other bits to the right one place. The rightmost bit is discarded. For example, 13 >> 1 (or binary 1101) shifted one place right results in 6 (binary 110).

2.4.10. Logical Operators

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:

Logical AND (&&, and)
The result of the logical AND operation is true if and only if both operands are true; otherwise, it is false. If the value of the first operand is false, the logical AND operator knows that the resulting value must also be false, so the righthand operand is never evaluated. This process is called short-circuiting, and a common PHP idiom uses it to ensure that a piece of code is evaluated only if something is true. For example, you might connect to a database only if some flag is not false:

$result = $flag and mysql_connect( );

The && and and operators differ only in their precedence.

Logical OR (||, or)
The result of the logical OR operation is true if either operand is true; otherwise, the result is false. Like the logical AND operator, the logical OR operator is short-circuited. If the lefthand operator is true, the result of the operator must be true, so the righthand operator is never evaluated. A common PHP idiom uses this to trigger an error condition if something goes wrong. For example:

$result = fopen($filename) or exit( );

The || and or operators differ only in their precedence.

Logical XOR (xor)
The result of the logical XOR operation is true if either operand, but not both, is true; otherwise, it is false.

Logical negation (!)
The logical negation operator returns the Boolean value true if the operand evaluates to false, and false if the operand evaluates to true.

2.4.11. Casting Operators

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.

Table 2-8. PHP casting operators

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.

2.4.12. Assignment Operators

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 &=.

2.4.12.2. Assignment with operation

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:

Plus-equals (+=)
Adds the righthand operand to the value of the lefthand operand, then assigns the result to the lefthand operand. $a += 5 is the same as $a = $a + 5.

Minus-equals (-=)
Subtracts the righthand operand from the value of the lefthand operand, then assigns the result to the lefthand operand.

Divide-equals (/=)
Divides the value of the lefthand operand by the righthand operand, then assigns the result to the lefthand operand.

Multiply-equals (*=)
Multiplies the righthand operand with the value of the lefthand operand, then assigns the result to the lefthand operand.

Modulus-equals (%=)
Performs the modulus operation on the value of the lefthand operand and the righthand operand, then assigns the result to the lefthand operand.

Bitwise-XOR-equals (^=)
Performs a bitwise XOR on the lefthand and righthand operands, then assigns the result to the lefthand operand.

Bitwise-AND-equals (&=)
Performs a bitwise AND on the value of the lefthand operand and the righthand operand, then assigns the result to the lefthand operand.

Bitwise-OR-equals (|=)
Performs a bitwise OR on the value of the lefthand operand and the righthand operand, then assigns the result to the lefthand operand.

Concatenate-equals (.=)
Concatenates the righthand operand to the value of the lefthand operand, then assigns the result to the lefthand operand.



Library Navigation Links

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







??????????????@Mail.ru