05.05.2013 Views

Programming PHP

Programming PHP

Programming PHP

SHOW MORE
SHOW LESS

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

Autoincrement and Autodecrement Operators<br />

In programming, one of the most common operations is to increase or decrease the<br />

value of a variable by one. The unary autoincrement (++) and autodecrement (––)<br />

operators provide shortcuts for these common operations. These operators are<br />

unique in that they work only on variables; the operators change their operands’ values<br />

as well as returning a value.<br />

There are two ways to use autoincrement or autodecrement in expressions. If you<br />

put the operator in front of the operand, it returns the new value of the operand<br />

(incremented or decremented). If you put the operator after the operand, it returns<br />

the original value of the operand (before the increment or decrement). Table 2-5 lists<br />

the different operations.<br />

Table 2-5. Autoincrement and autodecrement operations<br />

Operator Name Value returned Effect on $var<br />

$var++ Post-increment $var Incremented<br />

++$var Pre-increment $var + 1 Incremented<br />

$var-- Post-decrement $var Decremented<br />

--$var Pre-decrement $var – 1 Decremented<br />

These operators can be applied to strings as well as numbers. Incrementing an alphabetic<br />

character turns it into the next letter in the alphabet. As illustrated in Table 2-6,<br />

incrementing "z" or "Z" wraps it back to "a" or "Z" and increments the previous<br />

character by one, as though the characters were in a base-26 number system.<br />

Table 2-6. Autoincrement with letters<br />

Incrementing this Gives this<br />

"a" "b"<br />

"z" "aa"<br />

"spaz" "spba"<br />

"K9" "L0"<br />

"42" "43"<br />

Comparison Operators<br />

As their name suggests, comparison operators compare operands. The result is<br />

always either true, if the comparison is truthful, or false, otherwise.<br />

Operands to the comparison operators can be both numeric, both string, or one<br />

numeric and one string. The operators check for truthfulness in slightly different<br />

ways based on the types and values of the operands, either using strictly numeric<br />

comparisons or using lexicographic (textual) comparisons. Table 2-7 outlines when<br />

each type of check is used.<br />

This is the Title of the Book, eMatter Edition<br />

Copyright © 2002 O’Reilly & Associates, Inc. All rights reserved.<br />

Expressions and Operators | 39

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!