Operator Name Returns
True if…
Example Result
|| Or Left or right
is true
true || false true
or Or Left or right
is true
true || false true
xor Xor Left or right
is true but
not both
true || true false
&& And Left and
right are
true
true && false false
and And Left and
right are
true
true && false false
! Not The single
operand is
not true
! true false
Why are there two versions of both the or and the and operators? The answer lies in
operator precedence, which you will look at later in this section.
Automatically Incrementing and Decrementing an Integer Variable
When coding in PHP, you will often find it necessary to increment or decrement an
integer variable. You will usually need to do this when you are counting the
iterations of a loop. You have already learned two ways of doing this. I could
increment the integer contained by $x with the addition operator
$x = $x + 1; // $x is incremented
or with a combined assignment operator
$x += 1; // $x is incremented
In both cases, the resultant integer is assigned to $x. Because expressions of this
kind are so common, PHP provides some special operators that allow you to add or
subtract the integer constant 1 from an integer variable, assigning the result to the
variable itself. These are known as the post-increment and post-decrement
operators. The post-increment operator consists of two plus symbols appended to a
variable name.
$x++; // $x is incremented
increments the variable $x by one. Using two minus symbols in the same way
decrements the variable:
$x− − ; // $x is decremented
If you use the post-increment or post-decrement operators in conjunction with a
conditional operator, the operand will only be modified after the test has been
completed:
$x = 3;
$x++ < 4; // true
In the previous example, $x contains 3 when it is tested against 4 with the less than
operator, so the test expression returns true. After this test is complete, $x is
incremented.
In some circumstances, you might want to increment or decrement a variable in a
test expression before the test is carried out. PHP provides the pre -increment and
pre-decrement operators for this purpose. On their own, these operators behave in
exactly the same way as the post-increment and post-decrement operators. They
are written with the plus or minus symbols preceding the variable:
++$x; // $x is incremented
− − $x; // $x is decremented
If these operators are used as part of a test expression, the incrementation occurs
before the test is carried out.
$x = 3;
++$x < 4; // false
In the previous fragment, $x is incremented before it is tested against 4. The test
expression returns false because 4 is not smaller than 4.
Operator Precedence
When you use an operator, the interpreter usually reads your expression from left to
right. For complex expressions that use more than one operator, though, the waters
can become a little murky. First, consider a simple case:
4 + 5
There's no room for confusion, here. PHP simply adds 4 to 5. What about the next
fragment?
4 + 5 * 2
This presents a problem. Does it mean the sum of 4 and 5, which should then be
multiplied by 2, giving the result 18? Does it mean 4 plus the result of 5 multiplied
by 2, resolving to 14? If you were to read simply from left to right, the former would
be true. In fact, PHP attaches different precedence to operators. Because the
multiplication operator has higher precedence than the addition operator does, the
second solution to the problem is the correct one.
You can force PHP to execute the addition expression before the multiplication
expression with parentheses:
( 4 + 5 ) * 2
Whatever the precedence of the operators in a complex expression, it is a good idea
to use parentheses to make your code clearer and to save you from obscure bugs.
Table 4.6 lists the operators covered in this hour in precedence order (highest first).
Table 4.6: Order of Precedence for Selected Operators
Operators
++ − − (cast)
/ *%
+ −
< <= => >
== === !=
&&
||
= += − = /= *=%= .=
and
xor
or
As you can see, or has a lower precedence than || and and has a lower precedence
than &&, so you could use the lower-precedence logical operators to change the way
a complex test expression is read. This is not necessarily a good idea. The following
two expressions are equivalent, but the second is much easier to read:
$x and $y || $z
( $x && $y ) || $z
Constants
Variables offer a flexible way of storing data. You can change their values and the
type of data they store at any time. If, however, you want to work with a value that
you do not want to alter throughout your script's execution, you can define a
constant. You must use PHP's built-in function define() to create a constant. After
you have done this, the constant cannot be changed. To use the define() function,
you must place the name of the constant and the value you want to give it within the
call's parentheses:
define( "CONSTANT_NAME", 42 );
The value you want to set can only be a number or a string. By convention, the
name of the constant should be in capitals. Constants are accessed with the
constant name only; no dollar symbol is required. Listing 4.7 defines and accesses
a constan
No comments:
Post a Comment