PHP Operators With Examples

Today we will look into PHP operators. Earlier we went through PHP tutorial for beginners to get you started with variables and String.

PHP Operators

We can use PHP operators with String, integers, boolean and arrays.

PHP Operator Types

PHP scripting language provides different kinds of operators to work with variables such as arithmetic operators, comparison and logical operators.

1. PHP Arithmetic Operators

Operator Name Description
a+b Addition Sum of variables a and b, for example 2+3=5
a-b Subtraction Difference of a and b, for example 5-2=3
a*b Multiplication Product of a and b, for example 5*2=10
a/b Division Quotient of a and b, for example 10/2=5
a%b Modulus Remainder of a divided by b, for example 3%2=1
-a Negation Opposite of x, for example -5
a.b Concatenation Used to concat, for example “Pankaj” . “Kumar”=”PankajKumar”

2. PHP Assignment Operators

Operator Description
a=b The value of b will be set to a, the left side can be expressions also, for example a= 10-5
a+=b Same as a=a+b
a-=b Same as a=a-b
a*=b Same as a=a*b
a/=b Same as a=a/b
a%=b Same as a=a%b
a.=b Same as a=a.b

3. PHP Increment/Decrement Operators

Operator Name Description
++a Pre-increment Increment a by 1 and then returns it
a++ Post-increment returns a and then increment it by 1
–a Pre-decrement Decrement a by 1 and then returns it
a– Post-decrement returns a and then decrement it by 1

4. PHP Comparison Operators

Operator Name Description
a==b Equal True if a is equal to b
a===b Identical True if a is equal to b and of same type, 5===”5” is false
a!=b Not equal True if a and b are not equal
a<>b Not equal True if a and b are not equal
a!==b Not identical True if a and b are not equal and they are not of same type, for example 5!==”5″ returns true
a<b Less than True if a is less than b
a>b Greater than True if a is greater than b
a<=b Less than or equal to True if a is less than or equal to b
a>=b Greater than or equal to True if a is greater than or equal to b

5. PHP Logical Operators

Operator Name Description
a and b And True if both a and b are true
a or b Or True if either a or b is true
a xor b Xor True if either a or b is true, but not both
!a Not True if a is not true
a && b And Same as and operator
a || b Or Same as or operator

6. PHP Bitwise Operators

Operator Name Description
a & b And Bits that are set in both a and b are set.
a | b Or Bits that are set in either a or b are set
a ^ b Xor Bits that are set in a or b but not both are set
~ a Not Bits that are set in a are not set, and vice versa
a << b Shift left Shift the bits of a, b steps to the left
a >> b Shift right Shift the bits of a, b steps to the right

7. PHP Array Operators

Operator Name Description
a + b Union Union of a and b
a == b Equality True if a and b have same key/value pairs
a === b Identity True if a and b have the same key/value pairs in the same order and of the same types.
a != b Inequality True if a is not equal to b
a <> b Inequality True if a is not equal to b
a !== b Non-Identity True if a is not identical to b

8. PHP Type Operator

instanceof is the type operator used to determine if a PHP variable is an instantiated object of a class or not.

That’s all for PHP operators. In the next tutorial, we will learn about PHP Conditional Statements, Arrays and Loops in PHP.

By admin

Leave a Reply