What Is an Operation

This section describes what is an operation - a process that takes one or two operands and returns a result based on certain processing rule represented by the operator.

What Is an Operation? An operation is a process that takes one or two operands and returns a result based on certain processing rule represented by the operator. PHP supports most types of operations used in other programming languages:

1. Bitwise Operations: and (\$a & \$b), or (\$a | \$b), xor (\$a ^ \$b), not (~ \$a), shift left (\$a << \$s), and shift right (\$a >> \$s).

2. Incrementing/Decrementing Operations: pre-increment (++\$a), post-increment (\$a++), pre-decrement (--\$a), and post-decrement (\$a--).

3. Arithmetic Operations: negation (-\$a), addition (\$a + \$b), subtraction (\$a - \$b), multiplication (\$a * \$b), division (\$a / \$b), and modulus (\$a % \$b).

4. Comparison Operations: equal (\$a == \$b), identical (\$a === \$b), not equal (\$a != \$b, \$a <> \$b), not identical (\$a !== \$b), less than (\$a < \$b), greater than (\$a > \$b), less than or equal (\$a <= \$b), and greater than or equal (\$a >= \$b).

5. Logical Operations: and (\$a and \$b, \$a && \$b), or (\$a or \$b, \$a || \$b), xor (\$a xor \$b), and not (!\$a).

6. String Operation: concatenation (\$a . %b).

7. Assignment Operations: assignment (\$a = %b), addition with assignment (\$a += \$b), subtraction with assignment (\$a -= \$b), multiplication with assignment (\$a *= \$b), division with assignment (\$a /= \$b), modulus with assignment (\$a %= \$b), and concatenation with assignment (\$a .= %b).

To help us understand different types of operations, I wrote the following PHP script, OperationTest.php:

<?php
#  OperationTest.php
#
print "\n Bitwise Operations:\n";
print "    0x00FF & 0x3210: " . (0x00FF & 0x3210) . "\n";
print "    0x0001 | 0x0010: " . (0x0001 | 0x0010) . "\n";
print "    0x0001 << 4: " . (0x0001 << 4) . "\n";
print "    0x0100 >> 4: " . (0x0100 >> 4) . "\n";

print "\n Incrementing/Decrementing Operations:\n";
\$a = 10; \$b = ++\$a * 2;
print "    \\$b = ++\\$a * 2: \$a, \$b\n";
\$a = 10; \$b = \$a++ * 2;
print "    \\$b = \\$a++ *2: \$a, \$b\n";

print "\n Arithmetic Operations:\n";
\$a = 9; \$b = 4;
print "    \\$a * \\$b: ". \$a * \$b . "\n";
print "    \\$a * \\$b: ". \$a / \$b . "\n";
print "    \\$a * \\$b: ". \$a % \$b . "\n";

print "\n Comparison Operations:\n";
\$a = 9; \$b = 4;
print "    \\$a > \\$b: ". (\$a > \$b) . "\n";
print "    \\$a < \\$b: ". (\$a < \$b) . "\n";

print "\n Logical Operations:\n";
\$a = 9; \$b = 4; \$c = 1;
print "    \\$a > \\$b && \\$b > \\$c: ". (\$a > \$b && \$b > \$c) . "\n";

print "\n String Operation:\n";
\$name = "Herong"; \$greeting = "Hello";
print "    \\$greeting . \\$name: ". (\$greeting . \$name) . "\n";

print "\n Assignment Operation:\n";
print "    \\$name=\"Herong\": ". (\$name="Herong") . "\n";
?>

If you run this sample script, you should get:

C:\herong> \php\php OperationTest.php

Bitwise Operations:
0x00FF & 0x3210: 16
0x0001 | 0x0010: 17
0x0001 << 4: 16
0x0100 >> 4: 16

Incrementing/Decrementing Operations:
\$b = ++\$a * 2: 11, 22
\$b = \$a++ *2: 11, 20

Arithmetic Operations:
\$a * \$b: 36
\$a * \$b: 2.25
\$a * \$b: 1

Comparison Operations:
\$a > \$b: 1
\$a < \$b:

Logical Operations:
\$a > \$b && \$b > \$c: 1

String Operation:
\$greeting . \$name: HelloHerong

Assignment Operation:
\$name="Herong": Herong