Microsoft Word - Core PHP Programming Using PHP to Build Dynamic Web Sites

(singke) #1

++ (^) Increment
-- (^) Decrement
Addition, subtraction, multiplication, and division are familiar concepts. They may be
applied to integers or doubles. Using a string with an arithmetic operator causes the string
to be converted to a number first. Modulo division returns the integer remainder of a
division. The - operator may also be used to swap the sign on a number or variable.
The increment and decrement operators are shorthand for adding or subtracting 1 from a
variable. You might remember that we used it in Listing 2.3 inside the for loop. You
may put an increment or decrement operator before or after a variable. If the variable is
within an expression, you will get one of two behaviors. If an increment operator
precedes the variable, the variable will be incremented prior to evaluation of the
expression; otherwise the variable isn't operated on until after the value of the expression
is computed. Listing 2.7 demonstrates this concept.
Listing 2.7 Comparing Preincrement to Postincrement
<?
$VisitorsToday = 1;
// prints 1
print($VisitorsToday++);
// VisitorsToday is now 2
print("
\n");
// prints 3
print(++$VisitorsToday);
print("
\n");
?>
In this first print statement VisitorsToday still contains the value 1 when it is printed,
because the increment operator isn't applied until after the expression is evaluated. In the
third print statement VisitorsToday is incremented before the expression is evaluated;
therefore 3 is sent to the browser.
Logical and Relational Operators
Relational operators compare values and return either TRUE or FALSE. Logical operators
perform logical operations on TRUE and FALSE. Values used with a logical operator are
converted into booleans prior to being evaluated. For numerical values, zero will be
interpreted as FALSE, and other values will be TRUE. Empty strings are considered be
FALSE, and any nonempty string is TRUE. Table 2.5 lists the logical and relational
operators.
Table 2.5. Logical and Relational Operators
Operator Operation Performed

Free download pdf