O'Reilly logo

Programming PHP, 3rd Edition by Rasmus Lerdorf, Peter MacIntyre, Kevin Tatroe

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

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

Table 2-3. PHP operators

P

A

Operator

Operation

21

N

clone, new

Create new object

20

L

[

Array subscript

19

R

~

Bitwise NOT

 

R

++

Increment

 

R

−−

Decrement

 

R

(int), (bool), (float), (string), (array), (object), (unset)

Cast

 

R

@

Inhibit errors

18

N

instanceof

Type testing

17

R

!

Logical NOT

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

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required