What is Operators ?

Operators are used to perform operations on variables and values.

PHP Operator is a symbol i.e used to perform operations on operands.

For example: $num=10+20;//+ is the operator and 10,20 are operands
In the above example, + is the binary + operator, 10 and 20 are operands and $num is variable.

PHP Operators can be categorised in following forms:

  • Arithmetic Operators
  • Comparison Operators
  • Bitwise Operators
  • Logical Operators
  • String Operators
  • Incrementing/Decrementing Operators
  • Array Operators
  • Type Operators
  • Execution Operators
  • Error Control Operators
  • Assignment Operators

Arithmetic Operators

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

NameExampleResult
Identity+$aConversion of $a to int or float as appropriate.
Negation-$aOpposite of $a.
Addition$a + $bSum of $a and $b.
Subtraction$a – $bDifference of $a and $b.
Multiplication$a * $bProduct of $a and $b.
Division$a / $bQuotient of $a and $b.
Modulo$a % $bRemainder of $a divided by $b.
Exponentiation$a ** $bResult of raising $a to the $b‘th power. Introduced in PHP 5.6.

Comparison Operators

The PHP comparison operators are used to compare two values (number or string).

NameExampleResult
Equal$a ==$bTRUE if $a is equal to $b after type juggling.
Identical$a===$bTRUE if $a is equal to $b, and they are of the same type.
Not equal$a != $bTRUE if $a is not equal to $b after type juggling.
Not equal$a <>$bTRUE if $a is not equal to $b after type juggling.
Not identical$a!==$bTRUE if $a is not equal to $b, or they are not of the same type.
Less than$a < $bTRUE if $a is strictly less than $b.
Greater than$a > $bTRUE if $a is strictly greater than $b.
Less than or equal to$a <= $bTRUE if $a is less than or equal to $b.
Greater than or equal to$a >= $bTRUE if $a is greater than or equal to $b.
Spaceship$<=>$bAn integer less than, equal to, or greater than zero when $a is less than, equal to, or greater than $b, respectively. Available as of PHP 7.

Bitwise Operators

NameExampleResult
And$a & $bBits that are set in both $a and $b are set.
Or (inclusive or)$a | $bBits that are set in either $a or $b are set.
Xor (exclusive or)$a ^ $bBits that are set in $a or $b but not both are set.
Not~$aBits that are set in $a are not set, and vice versa.
Shift left$a<< $bShift the bits of $a $b steps to the left (each step means “multiply by two”)
Shift right$a>> $bShift the bits of $a $b steps to the right (each step means “divide by two”)

Logical Operators

The PHP logical operators are used to combine conditional statements.

NameExampleResult
And$a and $bTRUE if both $a and $b are TRUE.
Or$a or $bTRUE if either $a or $b is TRUE.
Xor$a xor $bTRUE if either $a or $b is TRUE, but not both.
Not! $aTRUE if $a is not TRUE.
And$a && $bTRUE if both $a and $b are TRUE.
Or$a || $bTRUE if either $a or $b is TRUE.

String Operators

There are two string operators. The first is the concatenation operator (‘.’), which returns the concatenation of its right and left arguments. The second is the concatenating assignment operator (‘.=’), which appends the argument on the right side to the argument on the left side.

NameExampleResult
Concatenationecho $str1.” ”.$str2;Combine Two String Temporary
Appends$str1 .= $str2

echo $str1;

Combine Str1 and Str2 into String Str1.

Incrementing/Decrementing Operators

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

NameExampleEffect
Pre-increment++$aIncrements $a by one, then returns $a.
Post-increment$a++Returns $a, then increments $a by one.
Pre-decrement–$aDecrements $a by one, then returns $a.
Post-decrement$a–Returns $a, then decrements $a by one.

Array Operators

The PHP array operators are used to compare arrays.

NameExampleResult
Union$a + $bUnion of $a and $b.
Equality$a == $bTRUE if $a and $b have the same key/value pairs.
Identity$a === $bTRUE if $a and $b have the same key/value pairs in the same order and of the same types.
Inequality$a != $bTRUE if $a is not equal to $b.
Inequality$a <> $bTRUE if $a is not equal to $b.
Non-identity$a !== $bTRUE if $a is not identical to $b.

Type Operators

In Type Operator  ” instanceof ” is used to determine whether a variable is an instantiated object of a certain class or Not.

NameExampleResult
instanceof$a instanceof MyClassTRUE if an instance object of a certain class

Execution Operators

Execution Operators in php supports one execution operator: backticks (“). PHP will attempt to execute the contents of the backticks as a shell command.

<?php
$output = `ls`;
echo "<pre>$output</pre>";
/*
Filehandle.php
test.php
test1.php
test2.php
*/
?>

Error Control Operators

Error Control Operators is used to skip error display in code.there is one error control operator with at sign (@). When prepended to an expression in PHP, any error messages that might be generated by that expression will be ignored.

<?php
// Not Declare Variables
$x = 10;
echo $x; // 10 
echo @$z; //nothing Show

Assignment operators

The PHP assignment operators are used with numeric values to write a value to a variable.

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.

Operator Precedence

AssociativityOperatorsAdditional Information
non-associativeclone newclone and new
right**arithmetic
right++  ~ (int) (float) (string)(array) (object) (bool) @types and increment/decrement
non-associativeinstanceoftypes
right!logical
left* / %arithmetic
left+  .arithmetic and string
left<< >>bitwise
non-associative< <= > >=comparison
non-associative== != === !== <> <=>comparison
left&bitwise andreferences
left^bitwise
left|bitwise
left&&logical
left||logical
right??null coalescing
left? :ternary
right= += -= *= **= /= .= %= &= |=^= <<= >>= ??=assignment
rightyield fromyield from
rightyieldyield
leftandlogical
leftxorlogical
leftorlogical

 

(Visited 213 times, 1 visits today)
Share with Friends :
Written by:

Leave a Reply

Your email address will not be published. Required fields are marked *