downloads | documentation | faq | getting help | mailing lists | reporting bugs | sites | links | my 
search for in the  
<ExpressionsArithmetic Operators>
view the version of this page
Last updated: Thu, 21 Aug 2003

Chapter 10. Operators

Operator Precedence

The precedence of an operator specifies how "tightly" it binds two expressions together. For example, in the expression 1 + 5 * 3, the answer is 16 and not 18 because the multiplication ("*") operator has a higher precedence than the addition ("+") operator. Parentheses may be used to force precedence, if necessary. For instance: (1 + 5) * 3 evaluates to 18.

The following table lists the precedence of operators with the lowest-precedence operators listed first.

Table 10-1. Operator Precedence

right = += -= *= /= .= %= &= |= ^= <<= >>=
left? :
non-associative== != === !==
non-associative< <= > >=
left<< >>
left+ - .
left* / %
right! ~ ++ -- (int) (float) (string) (array) (object) @

Note: Although ! has a higher precedence than =, PHP will still allow expressions similar to the following: if (!$a = foo()), in which case the output from foo() is put into $a.

add a note add a note User Contributed Notes
jlsalinas at NOSPAM dot gmx dot net
11-Jan-2003 02:14
It looks like a lot of people don't really understand what's an operator (probably they come from the HTML field). An operator is something that you feed with one or more values (or expressions in the programing jargon) and yields another value (so that the construction itself becomes an expression).

 So you can think of functions or constructions that return a value (like print) as operators and those that return nothing (like echo) as any other thing.

 This relates to the ternary operator, ?:, as well. It should be used to select between two expressions depending on a third one, not to select two sentences or paths of execution. And always sorrounding ?: expressions with parenthesis  is also a very good idea. As I've had to program in a variety of languages, I've got used to use parenthesis everywhere, and it's really worth it. They are only two kesystrokes, but they can save you a lot of headaches.

 I hope this will be useful to those of you who have to dive into PHP without a good programming ground. Don't desperate, it's really easy, and even a joy! ;)

<ExpressionsArithmetic Operators>
 Last updated: Thu, 21 Aug 2003
show source | credits | sitemap | mirror sites 
Copyright © 2001-2003 The PHP Group
All rights reserved.
This mirror generously provided by:
Last updated: Sat 01 Nov 2003 04:13:36 EST EST