Php operators and expressions
Php operators are punctuation symbols that process operands and produce corresponding values, While a snippet contains operands along with some operators is known as php expression. A php expression may contains variables and some constants.
Mainly php has six types of operator are as follows
- Arithmetic Operators
- Assignment Operators
- Incrementing/Decrementing Operators
- Comparison Operators
- Logical Operators
- Array Operators
Arithmatic Operator in php
Arithmetic operators in php are the mathematical operators that mainly used to perform basic mathematical operations such as addition, subtraction, multiplication etc.
Operator | Name | Description | Example | Result |
x + y | Addition | Sum of x and y | 2 + 2 | 4 |
x – y | Subtraction | Difference of x and y | 5 – 2 | 3 |
x * y | Multiplication | Product of x and y | 5 * 2 | 10 |
x / y | Division | Quotient of x and y | 15 / 5 | 3 |
x % y | Modulus | Remainder of x divided by y | 5 % 2 10 % 8 10 % 2 |
1 2 0 |
– x | Negation | Opposite of x | – 2 | |
a . b | Concatenation | Concatenate two strings | “Hi” . “Ha” | HiHa |
Assignment Operators in php
Assignment operators in php used to assign values of some variables to someone else. Assignment operators can be customized with arithmetic operators such as follows.
Assignment | Same as… | Description |
x = y | x = y | The left operand gets set to the value of the expression on the right |
x += y | x = x + y | Addition |
x -= y | x = x – y | Subtraction |
x *= y | x = x * y | Multiplication |
x /= y | x = x / y | Division |
x %= y | x = x % y | Modulus |
a .= b | a = a . b | Concatenate two strings |
Incrementing/Decrementing Operators in php
Increment operators in php and decrement operators in php are used to increment and decrement values of variables they can be pre –increment , post-increment, pre-decrement, post-decrement.
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 |
Comparison Operators in php
Comparison operators in php are used to compare two values and return true and false depends upon the conditions.
Operator | Name | Description | Example |
x == y | Equal | True if x is equal to y | 5==8 returns false |
x === y | Identical | True if x is equal to y, and they are of same type | 5===”5″ returns false |
x != y | Not equal | True if x is not equal to y | 5!=8 returns true |
x <> y | Not equal | True if x is not equal to y | 5<>8 returns true |
x !== y | Not identical | True if x is not equal to y, or they are not of same type | 5!==”5″ returns true |
x > y | Greater than | True if x is greater than y | 5>8 returns false |
x < y | Less than | True if x is less than y | 5<8 returns true |
x >= y | Greater than or equal to | True if x is greater than or equal to y | 5>=8 returns false |
x <= y | Less than or equal to | True if x is less than or equal to y | 5<=8 returns true |
Logical Operators in php
Logical operators are used to perform logical operations on variables or expressions logical operators may be AND, Or, XOR, &&, ||, !X .
Operator | Name | Description | Example |
x and y | And | True if both x and y are true | x=6 y=3 (x < 10 and y > 1) returns true |
x or y | Or | True if either or both x and y are true | x=6 y=3 (x==6 or y==5) returns true |
x xor y | Xor | True if either x or y is true, but not both | x=6 y=3 (x==6 xor y==3) returns false |
x && y | And | True if both x and y are true | x=6 y=3 (x < 10 && y > 1) returns true |
x || y | Or | True if either or both x and y are true | x=6 y=3 (x==5 || y==5) returns false |
! x | Not | True if x is not true | x=6 y=3 !(x==y) returns true |
Array Operators in php
Array operators are used to find union, equality, Identity etc. of the php expressions.
Operator | Name | Description |
x + y | Union | Union of x and y |
x == y | Equality | True if x and y have the same key/value pairs |
x === y | Identity | True if x and y have the same key/value pairs in the same order and of the same types |
x != y | Inequality | True if x is not equal to y |
x <> y | Inequality | True if x is not equal to y |
x !== y | Non-identity | True if x is not identical to y |