Книга: Fedora™ Unleashed, 2008 edition

Operators

Operators

Now that we have data values to work with, we need some operators to use, too. We have already used + to add variables together, but many others in PHP handle arithmetic, comparison, assignment, and other operators. Operator is just a fancy word for something that performs an operation, like addition or subtraction. However, operand might be new to you. Consider this operation:

$a = $b + c;

In this operation, = and + are operators and $a , $b , and $c are operands. Along with +, you also already know - (subtract), * (multiply), and / (divide), but Table 27.2 shows more.

TABLE 27.2 Operators in PHP

Operator What It Does
= Assigns the right operand to the left operand.
== Returns true if the left operand is equal to the right operand.
!= Returns true if the left operand is not equal to the right operand.
=== Returns true if the left operand is identical to the right operand. This is not the same as ==.
!== Returns true if the left operand is not identical to the right operand. This is not the same as !=.
< Returns true if the left operand is smaller than the right operand.
> Returns true if the left operand is greater than the right operand.
<= Returns true if the left operand is equal to or smaller than the right operand.
&& Returns true if both the left operand and the right operand are true.
|| Returns true if either the left operand or the right operand is true.
++ Increments the operand by one.
-- Decrements the operand by one.
+= Increments the left operand by the right operand.
-= Decrements the left operand by the right operand.
. Concatenates the left operand and the right operand (joins them).
% Divides the left operand by the right operand and returns the remainder.
| Performs a bitwise OR operation. It returns a number with bits that are set in either the left operand or the right operand.
& Performs a bitwise AND operation. It returns a number with bits that are set both in the left operand and the right operand.

There are at least 10 other operators not listed, but to be fair, you're unlikely to use them. Even some of the ones in this list are used infrequently — bitwise AND, for example. Having said that, the bitwise OR operator is used regularly because it allows you to combine values.

Here is a code example demonstrating some of the operators:

<?php
 $i = 100;
 $i++; // $i is now 101
 $i--; // $i is now 100 again
 $i += 10; // $i is 110
 $i = $i / 2; // $i is 55
 $j = $i; // both $j and $i are 55
 $i = $j % 11; // $i is 0
?>

The last line uses modulus, which for some people takes a little bit of effort to under stand. The result of $i % 11 is 0 because $i is set to 55 and modulus works by dividing the left operand (55) by the right operand (11) and returning the remainder. 55 divides by 11 exactly five times, and so has the remainder 0.

The concatenation operator, a period, sounds scarier than it is: It just joins strings together. For example:

<?php
 echo "Hello, " . "world!";
 echo "Hello, world!" . "n";
?>

Two "special" operators in PHP are not covered here and yet are used frequently. Before you look at them, though, it's important that you see how the comparison operators (such as <, <=, and !=) are used inside conditional statements.

Оглавление книги


Генерация: 0.035. Запросов К БД/Cache: 0 / 0
поделиться
Вверх Вниз