Skip to Main Content
Learning PHP, MySQL, and JavaScript
book

Learning PHP, MySQL, and JavaScript

by Robin Nixon
July 2009
Beginner content levelBeginner
526 pages
14h 24m
English
O'Reilly Media, Inc.
Content preview from Learning PHP, MySQL, and JavaScript

Operators

PHP offers a lot of powerful operators that range from arithmetic, string, and logical operators to assignment, comparison, and more (see Table 4-1).

Table 4-1. PHP operator types

Operator

Description

Example

Arithmetic

Basic mathematics

$a + $b

Array

Array union

$a + $b

Assignment

Assign values

$a = $b + 23

Bitwise

Manipulate bits within bytes

12 ^ 9

Comparison

Compare two values

$a < $b

Execution

Executes contents of backticks

`ls -al`

Increment/Decrement

Add or subtract 1

$a++

Logical

Boolean

$a and $b

String

Concatenation

$a . $b

Each operator takes a different number of operands:

  • Unary operators, such as incrementing ($a++) or negation (-$a), which take a single operand.

  • Binary operators, which represent the bulk of PHP operators, including addition, subtraction, multiplication, and division.

  • One ternary operator, which takes the form ? x : y. It’s a terse, single-line if statement that chooses between two expressions, depending on the result of a third one.

Operator Precedence

If all operators had the same precedence, they would be processed in the order in which they are encountered. In fact, many operators do have the same precedence, so let’s look at a few in Example 4-5.

Example 4-5. Three equivalent expressions
1 + 2 + 3 - 4 + 5
2 - 4 + 5 + 3 + 1
5 + 2 - 4 + 1 + 3

Here you will see that although the numbers (and their preceding operators) have been moved, the result of each expression is the value 7, because the plus and minus operators have the same precedence. We can try the same thing with multiplication ...

Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.
Start your free trial

You might also like

Web Database Applications with PHP and MySQL, 2nd Edition

Web Database Applications with PHP and MySQL, 2nd Edition

Hugh E. Williams, David Lane
Learning PHP

Learning PHP

David Sklar

Publisher Resources

ISBN: 9780596803605Supplemental ContentErrata Page