Page 79 - Beginning PHP 5.3
P. 79
Chapter 3: PHP Language Basics
Type Description
Comparison Compare values in a Boolean fashion ( true or false is
returned)
Error Control Affect error handling
Execution Cause execution of commands as though they were shell
commands
Incrementing/Decrementing Increment or decrement a variable ’ s value
Logical Boolean operators such as and , or , and not that can be used to
include or exclude
String Concatenates (joins together) strings (there ’ s only one string
operator)
Array Perform operations on arrays (covered in Chapter 6 )
In the remainder of this section, you explore the most frequently used PHP operators.
Arithmetic Operators
In PHP, the arithmetic operators (plus, minus, and so on) work much as you would expect, enabling you
to write expressions as though they were simple equations. For example, $c = $a + $b adds $a and $b
and assigns the result to $c . Here ’ s a full list of PHP ’ s arithmetic operators:
Operator Example Equation
+ (addition) 6 + 3 = 9
- (subtraction) 6 - 3 = 3
* (multiplication) 6 * 3 = 18
/ (division) 6 / 3 = 2
% (modulus) 6 % 3 = 0 (the remainder of 6/3)
Assignment Operators
You ’ ve already seen how the basic assignment operator ( = ) can be used to assign a value to a variable:
$test_var = 8.23;
It ’ s also worth noting that the preceding expression evaluates to the value of the assignment: 8.23. This is
because the assignment operator, like most operators in PHP, produces a value as well as carrying out
the assignment operation. This means that you can write code such as:
$another_var = $test_var = 8.23;
41
9/21/09 8:51:23 AM
c03.indd 41
c03.indd 41 9/21/09 8:51:23 AM