your image

PHP operators - javatpoint

javapoint
Related Topic
:- PHP

PHP Operators

PHP Operator is a symbol i.e used to perform operations on operands. In simple words, operators are used to perform operations on variables or values. For example:

  1. $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 categorized in following forms:

We can also categorize operators on behalf of operands. They can be categorized in 3 forms:

 

 

History of Java

  • Unary Operators: works on single operands such as ++, -- etc.
  • Binary Operators: works on two operands such as binary +, -, *, / etc.
  • Ternary Operators: works on three operands such as "?:".

Arithmetic Operators

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

OperatorNameExampleExplanation+Addition$a + $bSum of operands-Subtraction$a - $bDifference of operands*Multiplication$a * $bProduct of operands/Division$a / $bQuotient of operands%Modulus$a % $bRemainder of operands**Exponentiation$a ** $b$a raised to the power $b

The exponentiation (**) operator has been introduced in PHP 5.6.

Assignment Operators

The assignment operators are used to assign value to different variables. The basic assignment operator is "=".

OperatorNameExampleExplanation=Assign$a = $bThe value of right operand is assigned to the left operand.+=Add then Assign$a += $bAddition same as $a = $a + $b-=Subtract then Assign$a -= $bSubtraction same as $a = $a - $b*=Multiply then Assign$a *= $bMultiplication same as $a = $a * $b/=Divide then Assign
(quotient)$a /= $bFind quotient same as $a = $a / $b%=Divide then Assign
(remainder)$a %= $bFind remainder same as $a = $a % $b

Bitwise Operators

The bitwise operators are used to perform bit-level operations on operands. These operators allow the evaluation and manipulation of specific bits within the integer.

OperatorNameExampleExplanation&And$a & $bBits that are 1 in both $a and $b are set to 1, otherwise 0.|Or (Inclusive or)$a | $bBits that are 1 in either $a or $b are set to 1^Xor (Exclusive or)$a ^ $bBits that are 1 in either $a or $b are set to 0.~Not~$aBits that are 1 set to 0 and bits that are 0 are set to 1<<Shift left$a << $bLeft shift the bits of operand $a $b steps>>Shift right$a >> $bRight shift the bits of $a operand by $b number of places

Comparison Operators

Comparison operators allow comparing two values, such as number or string. Below the list of comparison operators are given:

OperatorNameExampleExplanation==Equal$a == $bReturn TRUE if $a is equal to $b===Identical$a === $bReturn TRUE if $a is equal to $b, and they are of same data type!==Not identical$a !== $bReturn TRUE if $a is not equal to $b, and they are not of same data type!=Not equal$a != $bReturn TRUE if $a is not equal to $b<>Not equal$a <> $bReturn TRUE if $a is not equal to $b<Less than$a < $bReturn TRUE if $a is less than $b>Greater than$a > $bReturn TRUE if $a is greater than $b<=Less than or equal to$a <= $bReturn TRUE if $a is less than or equal $b>=Greater than or equal to$a >= $bReturn TRUE if $a is greater than or equal $b<=>Spaceship$a <=>$bReturn -1 if $a is less than $b
Return 0 if $a is equal $b
Return 1 if $a is greater than $b

Incrementing/Decrementing Operators

The increment and decrement operators are used to increase and decrease the value of a variable.

OperatorNameExampleExplanation++Increment++$aIncrement the value of $a by one, then return $a$a++Return $a, then increment the value of $a by one--decrement--$aDecrement the value of $a by one, then return $a$a--Return $a, then decrement the value of $a by one

Logical Operators

The logical operators are used to perform bit-level operations on operands. These operators allow the evaluation and manipulation of specific bits within the integer.

OperatorNameExampleExplanationandAnd$a and $bReturn TRUE if both $a and $b are trueOrOr$a or $bReturn TRUE if either $a or $b is truexorXor$a xor $bReturn TRUE if either $ or $b is true but not both!Not! $aReturn TRUE if $a is not true&&And$a && $bReturn TRUE if either $a and $b are true||Or$a || $bReturn TRUE if either $a or $b is true

String Operators

The string operators are used to perform the operation on strings. There are two string operators in PHP, which are given below:

 

OperatorNameExampleExplanation.Concatenation$a . $bConcatenate both $a and $b.=Concatenation and Assignment$a .= $bFirst concatenate $a and $b, then assign the concatenated string to $a, e.g. $a = $a . $b

Array Operators

The array operators are used in case of array. Basically, these operators are used to compare the values of arrays.

OperatorNameExampleExplanation+Union$a + $yUnion of $a and $b==Equality$a == $bReturn TRUE if $a and $b have same key/value pair!=Inequality$a != $bReturn TRUE if $a is not equal to $b===Identity$a === $bReturn TRUE if $a and $b have same key/value pair of same type in same order!==Non-Identity$a !== $bReturn TRUE if $a is not identical to $b<>Inequality$a <> $bReturn TRUE if $a is not equal to $b

Type Operators

The type operator instanceof is used to determine whether an object, its parent and its derived class are the same type or not. Basically, this operator determines which certain class the object belongs to. It is used in object-oriented programming.

  1. <?php  
  2.     //class declaration  
  3.     class Developer  
  4.     {}  
  5.     class Programmer  
  6.     {}  
  7.     //creating an object of type Developer  
  8.     $charu = new Developer();  
  9.       
  10.     //testing the type of object  
  11.     if( $charu instanceof Developer)  
  12.     {  
  13.         echo "Charu is a developer.";  
  14.     }  
  15.     else  
  16.     {     
  17.         echo "Charu is a programmer.";  
  18.     }  
  19.     echo "</br>";  
  20.     var_dump($charu instanceof Developer);           //It will return true.  
  21.     var_dump($charu instanceof Programmer);       //It will return false.  
  22. ?>  

Output:

Charu is a developer.bool(true) bool(false)

Execution Operators

PHP has an execution operator backticks (``). PHP executes the content of backticks as a shell command. Execution operator and shell_exec() give the same result.

OperatorNameExampleExplanation``backticksecho `dir`;Execute the shell command and return the result.
Here, it will show the directories available in current folder.

Note: Note that backticks (``) are not single-quotes.

Error Control Operators

PHP has one error control operator, i.e., at (@) symbol. Whenever it is used with an expression, any error message will be ignored that might be generated by that expression.

 

OperatorNameExampleExplanation@at@file ('non_existent_file')Intentional file error

PHP Operators Precedence

Let's see the precedence of PHP operators with associativity.

OperatorsAdditional InformationAssociativityclone newclone and newnon-associative[array()left**arithmeticright++ -- ~ (int) (float) (string) (array) (object) (bool) @increment/decrement and typesrightinstanceoftypesnon-associative!logical (negation)right* / %arithmeticleft+ - .arithmetic and string concatenationleft<< >>bitwise (shift)left< <= > >=comparisonnon-associative== != === !== <>comparisonnon-associative&bitwise ANDleft^bitwise XORleft|bitwise ORleft&&logical ANDleft||logical ORleft?:ternaryleft= += -= *= **= /= .= %= &= |= ^= <<= >>= =>assignmentrightandlogicalleftxorlogicalleftorlogicalleft,many uses (comma)left

Comments