Skip to Main Content
PHP in a Nutshell
book

PHP in a Nutshell

by Paul Hudson
October 2005
Intermediate to advanced content levelIntermediate to advanced
372 pages
11h 35m
English
O'Reilly Media, Inc.
Content preview from PHP in a Nutshell

Chapter 6. Operators

In this chapter, we look at operators, which are the symbols such as + (adding), - (subtracting), and * (multiplying).

Operators are like functions in that they do something with values, but they use symbols rather than function names. In the equation 2 + 3, the 2 and the 3 are both operands, and the + is the operator. There are three types of operators: unary, binary, and ternary, which take one, two, and three operands respectively. As you can see, the + operator (used to add numerical values) is a binary operator, because it takes two variables as input.

Arithmetic Operators

The arithmetic operators handle basic numerical operations, such as addition and multiplication. The full list is shown in Table 6-1.

Table 6-1. The arithmetic operators

+

Addition

Returns the first value added to the second: $a + $b.

-

Subtraction

Returned the second value subtracted from the first: $a - $b.

*

Multiplication

Returns the first value multiplied by the second: $a * $b.

/

Division

Returns the first value divided by the second: $a / $b.

%

Modulus

Divides the first value into the second, then returns the remainder: $a % $b. This only works on integers, and the result will be negative if $a is negative.

+=

Shorthand addition

Adds the second value to the first: $a += $b. Equivalent to $a = $a + $b.

-=

Shorthand subtraction

Subtracts the second value from the first: $a -= $b. Equivalent to $a = $a - $b.

*=

Shorthand multiplication

Multiplies the first value ...

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

PHP Cookbook

PHP Cookbook

Eric A. Mann
Programming PHP

Programming PHP

Rasmus Lerdorf, Kevin Tatroe
Learning PHP

Learning PHP

David Sklar

Publisher Resources

ISBN: 0596100671Errata Page