In this lesson, you will learn how to use PHP operators to perform arithmetic and logic operations on the data. An operator takes one or more variable known as operands, to manipulate data items. PHP has followed the syntax of C and Perl for its operators.

**PHP Operators**

Below are the most utilized operators in PHP:

**Arithmetic Operators**

These operators are widely used in PHP. they are used for mathematical operations which require numeric values to execute the expression. The following are a list of arithmetic operators:

**Addition operator (+)** returns the sum of two variables/values

**Subtraction operator (-)** returns the difference between two variables/values

**Multiplication operator (*) **returns the product of two or more operands

**Division operator (/)** returns the quotient of two variables/values. The result could be an integer or a floating-point number

**Modulus (Known as Mod) operator (%)** returns the remainder of the division of the first operand by the second operand.

#### Example of Arithmetic Operators

<?php $x = 10; $y = 15; $add = $x + $y; $sub = $y - $x; $mul = $x * $y; $div = $y / $x; $rem = $y % $x; echo "x + y is ". $add. "<br/>"; echo "x - y is ". $sub. "<br/>"; echo "x * y is ". $mul. "<br/>"; echo "x / y is ". $div. "<br/>"; echo "x % y is ". $rem. "<br/>"; var_dump($div); var_dump($rem); ?>

**Output**

There are two operators to increase and decrease operands by one. These need only one variable.

**Increment operator (++)** returns the variable after adding 1 to the value.

#### Example of increment operator

<?php $x = 10; echo $x++; ?>

This code will display 10 as echo statement and increment operation will be performed afterward.

**Decrement operator (–)** returns the variable after subtracting 1 to the value.

#### Example of Decrement Operator

<?php $x = 10; echo $x--; ?>

This code will display 10 as echo statement and decrement operation will be performed afterward.

**Assignment Operators**

Assigning a value to a variable is where these operators are used. They could be simple as assigning the value to the performing arithmetic operation and then assigning values.

#### Example of Assignment Operators

Let’s say you have to swap values of two variables. The assignment operator $x = 10 will assign an integer value to $x. using $temp = $x will assign $x’s value 10 to $temp.

<?php $x = 10; $y = 20; echo "values of x and y before swapping values: ". $x. " ". $y. "<br/>"; $temp = $x; $x = $y; $y = $temp; echo "values of x and y after swapping values: ". $x. " ". $y; ?>

The operator += is to add the value on the right side to the variable on the left side.

The statement $x +=$y is equal to $x = $x +$y.

$count += 1; is equal to $count = $count + 1;

##### Similar operators in PHP

**Plus-equal** $x +=5; is $x = $x + 5;

**Minus-equal** $x -=5; is $x = $x – 5;

**Multiply-equal** $x *=5; is $x = $x * 5;

**Divide-equal** $x /=5; is $x = $x / 5;

**Modulus-equa**l $x %=5; is $x = $x % 5;

**PHP Operators Precedence**

To determine in which order operators are evaluated in a single mathematical operation. Each operator is assigned precedence. Some operators have equal precedence e.g. precedence of add (+) and subtract(-) is equal.

Let’s take a look at the following expression:

$ans = 2 + 2 * 2;

The answer is 6. The expression is evaluated from the right as the multiplication operator has higher precedence than the plus operator. If you want to evaluate the expression in your own order, simply put the expression inside parentheses ()

$ans = (2+2) *2 ;

**Identity operator (===)** returns true if both operands/values have the same data type.

**Inequality operator (!=)** returns true if both operands are not equal.

**Greater than (>)** returns true if the value on the left is greater than the value on the right.

**Greater than or equal (>=)** returns true if the value on the left is greater or equal to value on the right.

**Less than (<)** returns true if the value on the left is less than the value on the right side.

**Less than or equal (<=)** returns true if the value on the left is less than or equal to the value on the right.

**Logical Operators**

Logical operators are used in a logical expression. A logical operator treats its operand(s) as Boolean value and also return a Boolean value on evaluation. It is used to evaluate conditions, for example, you might say if the age is less than 18 don’t let the person sign in. In PHP, the code might look something like the following:

If($age < 18) echo “You can’t sign in”;

**PHP provides the following logical operators**

**Logical and operator (&&)** returns true if both operands are true, false otherwise

**Logical or operator (||)** returns true if any operand is an expression is true, false otherwise

**Logical XOR (xor)** returns true if either operand, but not both is true, false otherwise

**Logical negation (!)** returns true if the operand is false and false if the operand is true

#### String concatenation operator

PHP uses the dot (.) operator to append one string with another. Below is an example of how to join two or more strings:

<?php $user_name = "John"; echo "Greetings ". $user_name. "!"; ?>

**Bitwise operators**

Bitwise operators perform operations on the binary representations of the variables/values.

**And operator (&)** returns true if both bits are 1, otherwise 0

**Or operator (|)** returns 0 if both corresponding bits are 0, otherwise 1

**Exclusive or operator (^)** returns 1 if either bit, but not both bits are 1, otherwise 0

**Not operator (~)** changes bit 1 to 0 and 0 to 1 in the operand

**Shift left operator (<<)** shifts the bits in the left-side operand left by the number of places specified by the operand on the right side

**Shift right operator (>>)** shifts the bits in the left-side operand right by the number of places specified by the operand on the right side.

PHP Strings | Tutorial Home |
PHP If-Else |