PHP Operators

# PHP Operators

Today we will learn about the PHP Operators.

So read the article till the end and i will make sure you know everything about PHP Operators.

Before i proceed, i would request you to do SUBSCRIBE to ShoutMe360.com so that you can get regular updates.

So lets start…..

### What is Operator?

In simple words, Operators are used to perform operations on variables and values.

In PHP there are many types of operators, see below:

• Arithmetic operators
• Assignment operators
• Comparison operators
• Increment/Decrement operators
• Logical operators
• String operators
• Array operators

### PHP Arithmetic Operators

The PHP arithmetic operators are used with numeric values to perform common arithmetical operations, such as addition, subtraction, multiplication etc.

Assume variable x holds 2 and variable y holds 4 then −

 Operator Name Example Description Result + Addition \$x + \$y Adds two operands \$x and \$y 6 – Subtraction \$x – \$y Subtracts second operand from the first -2 * Multiplication \$x * \$y Multiply both operands 8 % Modulus \$y % \$x Modulus Operator and remainder of after an integer division 0 / Division \$y / \$x Divide numerator by de-numerator 2 ** Exponentiation \$x ** \$y Result of raising \$x to the \$y’th power (Introduced in PHP 5.6) 16

### PHP Assignment Operators

The basic assignment operator in PHP is “=”. It means that the left operand gets set to the value of the assignment expression on the right.
There are following assignment operators supported by PHP language-

Assume variable x holds 2 and variable y holds 4 then −

 Assignment Same as… Description Result \$x = \$y \$x = \$y The left operand gets set to the value of the expression on the right 4 \$x += \$y \$x = \$x + \$y The left operand gets set to the value of the expression on the right 6 \$x -= \$y \$x = \$x – \$y Subtract AND assignment operator, It subtracts right operand from the left operand and assign the result to left operand -2 \$x *= \$y \$x = \$x * \$y Multiply AND assignment operator, It multiplies right operand with the left operand and assign the result to left operand 8 \$x /= \$y \$x = \$x / \$y Divide AND assignment operator, It divides left operand with the right operand and assign the result to left operand 0 \$x %= \$y \$x = \$x % \$y Modulus AND assignment operator, It takes modulus using two operands and assign the result to left operand 7

### Comparison Operators

There are following comparison operators supported by PHP language
Assume variable x holds 2 and variable y holds 4 then −

Operator Name Example Result
== Equal \$x == \$y Returns true if \$x is equal to \$y
=== Identical \$x === \$y Returns true if \$x is equal to \$y, and they are of the same type
!= Not equal \$x != \$y Returns true if \$x is not equal to \$y
<> Not equal \$x <> \$y Returns true if \$x is not equal to \$y
!== Not identical \$x !== \$y Returns true if \$x is not equal to \$y, or they are not of the same type
> Greater than \$x > \$y Returns true if \$x is greater than \$y
< Less than \$x < \$y Returns true if \$x is less than \$y
>= Greater than or equal to \$x >= \$y Returns true if \$x is greater than or equal to \$y
<= Less than or equal to \$x <= \$y Returns true if \$x is less than or equal to \$y

### PHP Increment / Decrement Operators

The PHP increment operators are used to increment a variable’s value.
The PHP decrement operators are used to decrement a variable’s value.

Operator Name Description
++\$x Pre-increment Increments \$x by one, then returns \$x
\$x++ Post-increment Returns \$x, then increments \$x by one
– -\$x Pre-decrement Decrements \$x by one, then returns \$x
\$x- – Post-decrement Returns \$x, then decrements \$x by one

### PHP Logical Operators

The PHP logical operators are used to combine conditional statements.

Operator Name Example Result
and And \$x and \$y True if both \$x and \$y are true
or Or \$x or \$y True if either \$x or \$y is true
xor Xor \$x xor \$y True if either \$x or \$y is true, but not both
&& And \$x && \$y True if both \$x and \$y are true
|| Or \$x || \$y True if either \$x or \$y is true
! Not !\$x True if \$x is not true

### PHP String Operators

PHP has two operators that are specially designed for strings.

Operator Name Example Result
. Concatenation \$txt1 . \$txt2 Concatenation of \$txt1 and \$txt2
.= Concatenation assignment \$txt1 .= \$txt2 Appends \$txt2 to \$txt1

### PHP Array Operators

The PHP array operators are used to compare arrays.

Operator Name Example Result
+ Union \$x + \$y Union of \$x and \$y
== Equality \$x == \$y Returns true if \$x and \$y have the same key/value pairs
=== Identity \$x === \$y Returns true if \$x and \$y have the same key/value pairs in the same order and of the same types
!= Inequality \$x != \$y Returns true if \$x is not equal to \$y
<> Inequality \$x <> \$y Returns true if \$x is not equal to \$y
!== Non-identity \$x !== \$y Returns true if \$x is not identical to \$y

### Conditional Operator

There is one more operator called as conditional operator. This first evaluates an expression for a true or false value and then execute one of the two given statements depending upon the result of the evaluation. The conditional operator has this syntax −

Operator Description Example
? : Conditional Expression If Condition is true ? Then value X : Otherwise value Y

### Operators Categories

We can also categorized the operators we have discussed above into following categories −

Unary prefix operators, which precede a single operand.

Binary operators, which take two operands and perform a variety of arithmetic and logical operations.

Ternary Operators, The conditional operator, which takes three operands and evaluates either the second or third expression, depending on the evaluation of the first expression.

Assignment operators, which assign a value to a variable.

### Precedence of PHP Operators

Operator precedence determines the grouping of terms in an expression. This affects how an expression is evaluated. Certain operators have higher precedence than others; for example, the multiplication operator has higher precedence than the addition operator −

For example x = 5 + 6 * 2; Here x is assigned 17, not 20 because operator * has higher precedence than + so it first get multiplied with 6*2 and then adds into 5.

Category Operator Associativity
Unary ! ++ — Right to left
Multiplicative * / % Left to right
Additive + – Left to right
Relational < <= > >= Left to right
Equality == != Left to right
Logical AND && Left to right
Logical OR || Left to right
Conditional ?: Right to left
Assignment = += -= *= /= %= Right to left

Hope this explains you enough about the Operators.

This is Lalit signing off for today.

If you like this post please give a thumbs up and keep it sharing.

Hey don’t forget to SUBSCRIBE please so that you can get notification on new posts.