Introduction
- Expression:
- An expression is a bit of code that can be evaluated to produce an output value.
- The simplest expressions are literal values and variables. A literal value evaluates itself, while a variable evaluates to the value stored in the variable.
Definition
- An operator is a symbol that takes some values in the form of variables or values/constants (called operands) and does some specific operations on it.
Feature of Operators
- Operators are sometimes written as punctuation symbols.
- Some operators modify their operands, while most do not.
- Most of the operators of Php were borrowed from C and Perl.
Order of Precedence of Operators
- Order of Precedence is the order in which operators in an expression are evaluated depending on their relative precedence.
- For example,
2 + 4 * 3
Here, the addition and multiplication operators have different orders of precedence i.e., the multiplication operator has higher precedence than addition. Therefore, multiplication happens first then addition. Thus, the result would be 2 + 12, or 14. - The order of precedence of Php is –
Operators | Additional Information | Associativity |
clone new | clone and new | non-associative |
[ | array() | left (left to right) |
** | arithmetic | right (right to left) |
++ — ~ (int) (float) (string) (array) (object) (bool) @ | increment/decrement and types | right (right to left) |
instanceof | types | non-associative |
! | logical (negation) | right (right to left) |
* / % | arithmetic | left (left to right) |
+ – . | arithmetic and string concatenation | left (left to right) |
<< >> | bitwise (shift) | left (left to right) |
< <= > >= | comparison | non-associative |
== != === !== <> | comparison | non-associative |
& | bitwise AND | left (left to right) |
^ | bitwise XOR | left (left to right) |
| | bitwise OR | left (left to right) |
&& | logical AND | left (left to right) |
|| | logical OR | left (left to right) |
?: | ternary | left (left to right) |
= += -= *= **= /= .= %= &= |= ^= <<= >>= => | assignment | right (right to left) |
and | logical | left (left to right) |
xor | logical | left (left to right) |
or | logical | left (left to right) |
, | many uses (comma) | left (left to right) |
Operator Associativity in Php
- Operator associativity is the order in which operators with the same order of precedence in an expression are evaluated.
- For example –
2 / 2 * 2
Here, the division and multiplication operators have the same order of precedence, but the result of
the expression may be –
2 / (2 * 2) // 0.5 (when multiplication is considered first and)
(2 / 2) * 2 // 2 (when division is considered first.)
But the solution is to follow the operator associativity rule hence the division and multiplication operators are left-associative; this means that in cases of ambiguity, the operators are evaluated from left to right. Thus, in the above example, the correct result is 2.
Type of Operators
(A) On the basis of operands involve in evaluating an expression, operators are of three types. These are –
(a) Unary operators:
-
- An operator in an expression that operates on a single operand is called a unary operator.
- Examples are: ++ and — are unary operators.
(b) Binary operators:
-
- An operator in an expression that operates on two operands is called a binary operator.
- Most operators in Php are binary operators.
- Examples are: +, -, *, / etc.
(c) Ternary operators:
-
- An operator in an expression that operates on three operands is called a ternary operator.
- For example – ? :
(B) On the basis of function/operation performed by operands in an expression, operators are of the following types –
- Arithmetic Operators
- Arithmetic operators are used to perform common arithmetic operations such as addition, subtraction, multiplication, division, etc. with numeric values.
- The arithmetic operators mostly include binary operators.
- These operators require numeric values, and non-numeric values are converted into numeric values.
- Arithmetic operators are –
OperatorSymbolName of OperatorExampleDescription
+ Addition Operator $x + $y Addition of two operands x & y. – Subtraction Operator $x – $y Subtraction of two operands x & y. * Multiplication Operator $x* $y Product of operands x & y. / Division Operator $x / $y Division of operands (Quotient) x & y. % Modulus Operator $x % $y The remainder of operands. ** Exponentiation Operator $x ** $y $x raised to the power $y[xy].
+ Arithmetic Assertion +(7 − 9) To represent/look expression +ive, not results.
– Arithmetic Negation −(5 − 8) To represent/look expression -ive, not results.
- Comparison/Conditional Operators
- Comparison operators are allowed to compare two values (either number or string).
- Comparison operators are –
Operator Symbol Name of Operator Example Description == Equal Operator $x == $y Return TRUE if $x is equal to $y === Identical/Absolute Equal Operator $x === $y Return TRUE if $x is equal to $y, and they are of the same data type too. !== Not identical Operator $x !== $y Return TRUE if $x is not equal to $y, and they are not of the same data type too. != Not equal Operator $x != $y Return TRUE if $x is not equal to $y <> Not equal Operator $x <> $y Return TRUE if $x is not equal to $y < Less than Operator $x < $y Return TRUE if $x is less than $y > Greater than Operator $x > $y Return TRUE if $x is greater than $y <= Less than or equal to the Operator $x <= $y Return TRUE if $x is less than or equal $y >= Greater than or equal to the Operator $x >= $y Return TRUE if $x is greater than or equal to $y <=> Spaceship Operator $x <=>$y Return -1 if $x is less than $y
Return 0 if $x is equal $y
Return 1 if $x is greater than $y?? Null coalescing operator $x ?? $y Return to the righthand operand if the lefthand operand is NULL;
otherwise, it returns to the lefthand operand.
Null Coalescing Operator
$x= null;
$y= 100;
echo $x ?? $y; //outputs 100
$x= 500;
$y= 100;
echo $x ?? $y; //outputs 500
- Logical Operators
- The logical operators are used to perform bit-level operations on operands in an expression.
- Logical operators treat their operands as Boolean values and return a Boolean value.
- The logical operators are –
Operator Symbol Name of Operator Example Description and/&&And Operator$x and $y$x && $yReturn TRUE if both $x and $y are trueOr/||Or Operator$x or $y$x || $yReturn TRUE if either $x or $y is truexor Xor Operator $x xor $y Return TRUE if either $x or $y is true but not both ! Not Operator ! $x Return TRUE if $x is not true
- Unary (Increment/Decrement) Operators
- The Unary operators are used to increase or decrease the value of a variable in an expression.
- The Unary Operators are –
OperatorSymbol
Name of Operator Example Description ++ Pre-Increment(++$x) & Post Increment($x++) ++$x Increment the value of $x by one first, then return $x $x++ Return $x first, then increment the value of $x by one — Pre-decrement (–$x) & Post- decrement ($x–) –$x Decrement the value of $x by one first, then return $x $x– Return $x first, then decrement the value of $x by one
- Array Operators
- The array operators are used in the case of an array.
- Basically, Array operators are used to comparing the values of arrays.
- The Array Operators are –
Operator Symbol Name of Operator Example Description + Union Operator $x + $y Union of $x and $y == Equality Operator $x == $y Return TRUE if $x and $y have the same key/value pair !=/<> Inequality Operator $x != $y Return TRUE if $x is not equal to $y === Identity Operator $x === $y Return TRUE if $x and $y have the same key/value pair of the same type in the same order !== Non-Identity Operator $x !== $y Return TRUE if $x is not identical to $y
- String/Concatenation Operators
- The string operators are used to perform the operation on strings in an expression.
- The string operators are –
OperatorSymbol
Name of Operator Example Description . ConcatenationOperator$x . $y Concatenate both the String $x and $y as one .= Concatenation and Assignment Operator $x .= $y First, concatenate $x and $y String as one, then assign the concatenated string to $x,e.g. $x = $x . $y
- Bitwise Operators
- The bitwise operators are used to perform bit-level operations on operands in an expression.
- The bitwise operators act on the binary representation of their operands. Each operand is first turned into a binary representation of the value.
- All the bitwise operators work on numbers as well as strings, but they vary in their treatment of string operands of different lengths.
- These operators allow the evaluation and manipulation of specific bits within the integer value.
- The bitwise operators are –
-
OperatorsSymbol
Name of Operators Example Description & And Operator $x & $y Bits that are 1 in both $x and $y are set to 1, otherwise 0. | Or (Inclusive or) Operator $x | $y Bits that are 1 in either $x or $y are set to 1 ^ Xor (Exclusive or) Operator $x ^ $y Bits that are 1 in either $x or $y are set to 0. ~ Not Operator ~$x Bits that are 1 are set to 0 and bits that are 0 are set to 1 << Shift left Operator $x << $y Left shift the bits of operand $x $y steps >> Shift right Operator $x >> $y Right shift the bits of $x operand by $y number of places
- Assignment & Shorthand Operators
- The assignment operators are used to assign (copy) values to the variables.
- The assignment operators are –
Operators Symbol Name of Operators Example Description = Assignment Operator $x = $y The value of the left operand($x) is assigned by the right operand($y). += Add then Assignment Operator $x += $y Addition works as $x= $x + $y -= Subtract then Assignment Operator $x -= $y Subtraction works as $x = $x – $y *= Multiply then Assignment Operator $a *= $b Multiplication works as $x = $x * $y /= Divide(quotient) then Assignment Operator $a /= $b Quotient Division works as $x = $x / $y %= Divide (remainder) then Assignment Operator $a %= $b Remainder Division works as $x = $x % $y
- Ternary/Conditional Operator
- Also called a conditional operator.
- It is considered the most underused operator.
- The conditional operator evaluates the expression before the ?. If the expression is true, the operator returns the value of the expression between the ? and : ; otherwise, the operator returns the value of the expression after the : symbol.
- For instance:
- $x=if($m>$n) ? $m : $n;
In this, if the condition is true then the value of $m would be assigned in $x variable otherwise $n.
- Instanceof Operator
- The instanceof the operator checks/confirm whether a variable is an (instantiated) object of a given particular class or implements an interface. For example-
$obj1 = new Employee; // object creation obj1 in class Employee.
$val1 = $obj1 instanceof Employee; // gives true
$val2 = $obj1 instanceof Student; // gives false
- The instanceof the operator checks/confirm whether a variable is an (instantiated) object of a given particular class or implements an interface. For example-
- Execution/Backtick Operator
- The backtick operator executes the string/command contained between the backticks (‘…’) as a shell command and returns the output. For example:-
$str= `ls -ls /tmp` ;
echo $str;
- The backtick operator executes the string/command contained between the backticks (‘…’) as a shell command and returns the output. For example:-
- Type Casting Operator
- Although PHP is a weakly typed language, even though it performs Type Casting.
- The conversion of a value from one type to another is called casting.
- Type Casting Operator allows us to force a value of a specific type into another particular data type as per need.
- The casting operators in Php are – (int), (float), (string), (bool), (array), (object), and (unset – to assign NULL value]).
- To use a casting operator, we first put the operator to the left of the operand and the type to which the operator changes the value.
- For example, the code:-
$x = “5”; Here, $x puts string value.
$y = (int) $x; Here, after casting $y stores integer value.
- Error Control/Error Suppression Operators
- In Php, some operators or functions can generate error messages which may be ignorable. Hence, the error suppression operator can be used to prevent these messages from being created.
- For this, Php has one warning control operator called at (@) symbol. Whenever it is used in the beginning of an expression, a warning message will be ignored that might be generated during the execution.
- The error control operator is –
Operator Symbol Name of Operator Example Description @ at @ —–source codes Ignore warning messages when arising during execution
0 Comments