×

  PHP

Introduction Getting Started Data Types Operators Conditional Statements Loops Functions Arrays Include Files GET and POST

  Mysql

Data Base Concepts Mysql Talking to Mysql

  PHP & Mysql

Connect DB Mysql insert Mysql select Mysql update Mysql delete
XAMPP Lite S/W

Operators

A numeric operator is a symbol that makes the script perform a specific mathematical or logical manipulation. The numeric operators can be grouped into five types: arithmetic, assignment, comparison, logical, and bitwise operators.

In the expression 4 + 5 is equal to 9. Here 4 and 5 are called operands and + is called operator.

Arithmetic Operators

The arithmetic operators include the four basic arithmetic operations, as well as the modulus operator (%), which is used to obtain the division remainder.

$x = 4 + 2; // 6, addition
$x = 4 - 2; // 2, subtraction
$x = 4 * 2; // 8, multiplication
$x = 4 / 2; // 2, division
$x = 4 % 2; // 0, modulus (division remainder)
$x = 4 ** 2; // 16, exponentiation

Assignment Operators

The second group is the assignment operators, most importantly, the assignment operator (=) itself, which assigns a value to a variable.

$x = 1; // assignment

Combined Assignment Operators

A common use of the assignment and arithmetic operators is to operate on a variable and then to save the result back into that same variable. These operations can be shortened with the combined assignment operators.

$x += 5; // $x = $x+5;
$x -= 5; // $x = $x-5;
$x *= 5; // $x = $x*5;
$x /= 5; // $x = $x/5;
$x %= 5; // $x = $x%5;
$x **= 5; // $x = $x**5;

Increment and Decrement Operators

Another common operation is to increment or decrement a variable by one. This can be simplified with the increment (++) and decrement (--) operators.

$x++; // $x += 1;
$x--; // $x -= 1;

Both of these operators can be used either before or after a variable.

$x++; // post-increment
$x--; // post-decrement
++$x; // pre-increment
--$x; // pre-decrement

The result on the variable is the same whichever is used. The difference is that the post-operator returns the original value before it changes the variable, whereas the pre-operator changes the variable first and then returns the value.

$x = 5; $y = $x++; // $x=6, $y=5
$x = 5; $y = ++$x; // $x=6, $y=6

Comparison Operators

The comparison operators compare two values and return either true or false. They are mainly used to specify conditions, which are expressions that evaluate to either true or false.

$x = (2 == 3); // equal to (false)
$x = (2 != 3); // not equal to (true)
$x = (2 <> 3); // not equal to (alternative)
$x = (2 === 3); // identical (false)
$x = (2 !== 3); // not identical (true)
$x = (2 > 3); // false,greater than (false)
$x = (2 < 3); // less than (true)
$x = (2 >= 3); // greater than or equal to (false)
$x = (2 <= 3); // less than or equal to (true)

The strict equality operators, === and !==, are used for comparing both type and value. These are necessary because the regular "equal to" (==) and "not equal to" (!=) operators automatically perform a type conversion before they compare the operands. It is considered good practice to use strict comparison when the type conversion feature of the "equal to" operation is not needed.

$x = (1 == "1"); // true (same value)
$x = (1 === "1"); // false (different types)

PHP 7 added a new comparison operator called the spaceship operator (<=>). It compares two values and returns 0 if both values are equal, 1 if the value on the left side is greater, and –1 if the value on the right side is greater.

$x = 1 <=> 1; // 0 (1 == 1)
$x = 3 <=> 2; // 1 (3 > 2)
$x = 1 <=> 2; // -1 (1 < 2)

Logical Operators

The logical operators are often used together with the comparison operators. Logical and (&&) evaluates to true if both the left and right side are true, and logical or (||) evaluates to true if either the left or right side is true. For inverting a Boolean result, there is the logical not (!) operator. Note that for both “logical and” and “logical or”, the right side of the operator is not evaluated if the result is already determined by the left side.

$x = (true && false); // logical and (false)
$x = (true || false); // logical or (true)
$x = !true; // logical not (false)

Bitwise Operators

The bitwise operators can manipulate binary digits of numbers. For example, the xor operator (^) turns on the bits that are set on one side of the operator, but not on both sides.

$x = 5 & 4; // 101&100=100 (4) // and
$x = 5 | 4; // 101|100=101 (5) // or
$x = 5 ^ 4; // 101^100=001 (1) // xor (exclusive or)
$x = 4 << 1; // 100<<1=1000 (8) // left shift
$x = 4 >> 1; // 100>>1=10 (2) // right shift
$x = ~4; // ~00000100=11111011 (-5) // invert

These bitwise operators have shorthand assignment operators, just like the arithmetic operators.

$x=5; $x &= 4; // 101&100=100 (4) // and
$x=5; $x |= 4; // 101|100=101 (5) // or
$x=5; $x ^= 4; // 101^100=001 (1) // xor
$x=5; $x <<= 1; // 101<<1=1010 (10)// left shift
$x=5; $x >>= 1; // 101>>1=10 (2) // right shift

Note that decimal numbers used together with bitwise operators are automatically evaluated in binary. The binary notation may also be used to specify binary numbers for a bitwise operation.

$x = 0b101 & 0b100; // 0b100 (4)

Operator Precedence

When an expression contains multiple operators, the precedence of those operators decides the order in which they are evaluated.

Order of Operator Precedence

To give an example, multiplication has greater precedence than addition, and therefore it is evaluated first in the following line of code.

$x = 4 + 3 * 2; // 10

Parentheses can be used to force precedence. An expression placed within parentheses is evaluated before other expressions in that statement.

$x = (4 + 3) * 2; // 14

Additional Logical Operators

In the precedence table, make special note of the last three operators: and, or, and xor. The and and or operators work in the same way as the logical && and || operators. The only difference is their lower level of precedence.

// Same as: $x = (true && false);
$x = true && false; // $x is false (0)
// Same as: ($x = true) and false;
$x = true and false; // $x is true (1)

The xor operator is a Boolean version of the bitwise ^ operator. It evaluates to true if only one of the operands is true.

$x = (true xor true); // false