PHP Tutorials - Herong's Tutorial Examples - v5.17, by Herong Yang
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 #- Copyright 2003 (c) HerongYang.com. All Rights Reserved. # 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:
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
Table of Contents
Introduction and Installation of PHP
PHP Data Types and Data Literals
Variables, References, and Constants
►Expressions, Operations and Type Conversions
Data Type Automatic Conversion
Conditional Statements - "if" and "switch"
Loop Statements - "while", "for", and "do ... while"
Function Declaration, Arguments, and Return Values
Interface with Operating System
Introduction of Class and Object
Integrating PHP with Apache Web Server
Retrieving Information from HTTP Requests
Creating and Managing Sessions in PHP Scripts
Sending and Receiving Cookies in PHP Scripts
Controlling HTTP Response Header Lines in PHP Scripts
MySQL Server Connection and Access Functions
Functions to Manage Directories, Files and Images
SOAP Extension Function and Calling Web Services
SOAP Server Functions and Examples
Localization Overview of Web Applications
Using Non-ASCII Characters in HTML Documents
Using Non-ASCII Characters as PHP Script String Literals
Receiving Non-ASCII Characters from Input Forms
"mbstring" Extension and Non-ASCII Encoding Management
Managing Non-ASCII Character Strings with MySQL Servers
Parsing and Managing HTML Documents
Configuring and Sending Out Emails
Managing PHP Engine and Modules on macOS