Assignment Operators
We use the assignment operator for assigning values to different variables.
-
Assign (=) - Assigns the value of the correct variable to the left variable.
-
Add then Assign (+=) - Addition done as $a = $a + $b.
-
Subtract then Assign (-=) - Subtraction done as $a = $a - $b.
-
Multiply then Assign (*=) - Multiplication done as $a = $a * $b.
-
Divide then Assign quotient (/=) - Gives quotient as $a = $a / $b.
-
Divide then Assign reminder (%=) - Gives reminder as $a = $a % $b.
Bitwise Operators
We use Bitwise operators to perform bit-level operations on operands.
-
AND (&) - Bits that are 1 in both $a and $b are set to 1, else set to 0.
-
OR (&) - Bits that are either 1 in $a and $b are set to 1, else set to 0.
-
XOR (^) - Bits that are 1 in both $a and $b are set to 0, else set to 1.
-
NOT (~) - Bits that are 1 are set to 0, and 0 is set to 1.
-
Left shift (<<) - Left shift the bits of $a by $b steps.
-
Right shift (>>) - Right shift the bits of $a by $b steps.
Read about Bitwise Operators in C here.
Comparison Operators
We use comparison operators for comparing two values such as number or string.
-
Equal (==) - Returns TRUE if $a is equal to $b.
-
Identical (===) - Returns TRUE if $a is equal to $b, along with their data types.
-
Not Equal (!=) - Returns TRUE if $a is not equal to $b.
-
Not Equal (<>) - Returns TRUE if $a is not equal to $b.
-
Not Identical (!==) - Returns TRUE if $a is not equal to $b, along with their data types are not equal.
-
Less than (<) - Returns TRUE if $a is less than to $b.
-
Greater than (>) - Returns TRUE if $a is greater than to $b.
-
Less than or equal to (<=) - Returns TRUE if $a is less than or equal to $b.
-
Greater than or equal to (>=) - Returns TRUE if $a is greater than or equal to $b.
-
Spaceship (<=>) - Return -1 if $a is less than $b, 0 if $a is equal $b, 1 if $a is greater than $b.
Incrementing/Decrementing Operators
We use these operators for incrementing and decrementing the value of a variable.
-
Post Increment (++) - Increments the value of $a by one, then return $a.
-
Pre Increment (++) - Return $a, thenIncrements the value of $a by one.
-
Post Decrement (--) - Decrements the value of $a by one, then returns $a.
-
Pre Decrement (--) - Return $a, then decrements the value of $a by one.
Logical Operators
We use logical operators to perform bit-level operations on operands.
-
And (and, &&) - Return TRUE if $a is equal to $b.
- For Example: $a and $b
- For Example: $a && $b
-
Or (or, ||) - Return TRUE if either $a or $b or both are true.
- For Example: $a or $b
- For Example: $a || $b
-
Xor (xor) - Return TRUE if either $a or $b are true, but not both.
-
Not (!) - Return TRUE if $a is not true.
String Operators
We use string operators to perform operations on strings.
-
Concatenation (.) - Concatenate both strings $a and $b.
-
Concatenation and Assignment (.=) - Concatenate both strings $a and $b, then assign it to $a like $a = $a.$b
Array Operators
We use array operators to compare the values of arrays.
-
Union (+) - Union of $a and $b.
-
Equality (==) - Return TRUE if $a and $b have same key/value pair.
-
Inequality (!=) - Return TRUE if $a is not equal to $b.
-
Identity (===) - Return TRUE if $a and $b have same key/value pair of same type in same order.
-
Non-Identity (!==) - Return TRUE if $a is not identical to $b.
-
Inequality (<>) - Return TRUE if $a is not equal to $b.
Execution Operators
PHP uses backticks to run shell commands.
-
Backticks(``) - Execute the shell command and return the result.
Error Control Operators
When we use the error control operator with an expression, the error message gets ignored by that expression.
-
at(@) - Intentional file error.
- For Example: @file('file_not_found')
Conditional Assignment Operators
Conditional Assignment Operators are used to setting values based on the following situations.
-
Ternary(?:) - Returns the value of $a, expr2 if expr1 = TRUE or expr3 if expr1 = FALSE.
- For Example: $a = expr1 ? expr2 : expr3
-
Null coalescing (??) - Returns the value of $a, expr1 if expr1 exists and not NULL, else expr2.
- For Example: $a = expr1 ?? expr2
Frequently Asked Questions
1. What is PHP?
PHP is a backend scripting language used for web development. It is an open-source, interpreted server-side scripting language and supports object-oriented programming.
2. What are the advantages of using PHP?
There are many advantages of using PHP over other languages. PHP is a server-side scripting language suited for creating dynamic web pages. It is highly flexible and provides compatibility and easy integration. It provides efficiency in performance and is cost-efficient.
3. What are PHP Operators?
PHP Operators are symbols that we use to operate the value of a variable or a value. There are many PHP operators like arithmetic, logical, conditional, relational, bitwise, assignment operators, etc.
Key Takeaways
This article teaches about PHP Operators and how we use them. We saw why it could be beneficial for a developer to learn. Click here to read about PHP Interview Questions.
Click here to see other related blogs on PHP.
Also, check out our web development course and blogs on Backend Web Technologies.
If you are preparing for your DSA interviews then, Coding Ninjas Studio is a one-stop destination. This platform will help you acquire effective coding techniques and overview student interview experience in various product-based companies.
By Abhay Trivedi