Core PHP PHP

PHP Operators

PHP Operators are do some thin between one or more variables or values.

Following is PHP operators:

  • Operator Precedence
  • Arithmetic operators
  • Assignment operators
  • Bitwise Operators
  • Comparison operators
  • Error Control Operators
  • Increment/Decrement operators
  • Logical operators
  • String operators
  • Array operators
  • Type Operators

PHP Operator Precedence

 

Precedence as name suggest its prform on the basiss of priority or primacy. That means if you want calculate some values with plus, substract, multiply or divide its prform first with high priority operators.

Example

5+6*3 = 23 Wright

5+6*3 = 33 Wrong

Note – Multiplication (*) operator has hight priority or precedence to its

(5+6)*3 = 33 Wright

 

Associativity Operators
non-associative clone new
Left [
Right **
Right ++ ~ (int) (float) (string) (array) (object) (bool) @
non-associative Instanceof
Right !
Left * / %
Left + .
Left << >>
non-associative < <= > >=
non-associative == != === !== <> <=>
Left &
Left ^
Left |
Left &&
Left ||
Right ??
Left ? :
Right = += -= *= **= /= .= %= &= |= ^= <<= >>=
Left And
Left Xor
Left Or

 

PHP Arithmetic Operators

 

PHP arithmetic operators are used for numeric or mathematical. For example additions, subtractions, division, multiplication, modulus etc.

 

Operator Name Example Result
+ Addition $a + $b Sum value of $a and $b
Subtraction $a – $b Difference value of $a and $b
* Multiplication $a * $b Multiple value of $a and $b
/ Division $a / $b Division or quotient value of $a and $b
% Modulus $a % $b Remainder value of $a divided by $b
** Exponentiation $a ** $b Value of raising $a to the $b’th power (Introduced in PHP 5.6+)

 

PHP Assignment Operators

 

PHP assignment operators are used for assigning value from right to left variable or you can say that assing right side one or multiple variables value to left side one variable.

Basic assignment opertor of PHP is “=”.

 

Assignment Equalts Description
a = b a = b left operand gets value of right operand
a += b a = a + b Addition
a -= b a = a – b Subtraction
a *= b a = a * b Multiplication
a /= b a = a / b Division
a %= b a = a % b Modulus

 

PHP Bitwise Operators

Bitwise operators allow evaluation and manipulation of specific bits within an integer value.

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

 

The Comparison Operators

Comparison operators are used for compare 2 string or number values.

Operator Name Example Description
== Equal $a == $b Returns true if $a is equals to $b
=== Identical $a === $b Returns true if $a is equals to $b, and same type
!= Not Equal $a != $b Returns true if $a is not equals to $b
<> Not Equal $a <> $b Returns true if $a is not equals to $b
!== Not Identical $a !== $b Returns true if $a is not equal to $b, or not same type
> Greater Than $a > $b Returns true if $a greater than $b
< Less Than $a < $b Returns true if $a less than $b
>= Greater than or equal to $a >= $b Returns true if $a is greater than or equals to $b
<= Less than or equal to $a <= $b Returns true if $a is less than or equal to $b

 

PHP Error Control Operators

The at sign (@) use to contorl error. The error messages will be ignored.

<?php
$fileVar = @file(‘file_not_exist’);
?>

 

PHP Increment / Decrement Operators

 

Operator Name Description
++$a Pre increment Increments $a value 1, then returns $a
$a++ Post increment Returns $a, then increments $a value 1
–$a Pre decrement Decrements $a value 1, then returns $a
$a– Post decrement Returns $a, then decrements $a value 1

 

PHP Logical Operators

Operator Name Example Description
and And $a and $b True if both $a and $b are true
or Or $a or $b True if any one $a or $b is true
xor Xor $a xor $b True if any one $a or $b is true, but not both
&& And $a && $b True if both $a and $b are true
|| Or $a || $b True if any one $a or $b is true
! Not !$a True if $a is not true

 

PHP String Operators

Operator Name Example Description
. Concatenation $a . $b Concatenation of $a and $b
.= Concatenation assignment $a .= $b Append $a to $b

 

PHP Array Operators

array operators are used to compare two arrays.

Operator Name Example Description
+ Union $a + $b Union of $a and $b
== Equality $a == $b Returns true if $a and $b have same key=>value pairs
=== Identity $a === $b Returns true if $a and $b have same key=>value pairs in the same orders and same types
!= Inequality $a != $b Returns true if $a is not equals to $b
<> Inequality $a <> $b

 

Returns true if $a is not equals to $b
!== Non identity $a !== $b Returns true if $a is not identical to $b

 

PHP Type Operators

instanceof is used for determine.

Example

<?php
class A
{
}

class B
{
}
$b = new B;

var_dump($b instanceof A);
var_dump($b instanceof B);
?>

output:

bool(false)

bool(true)

Leave a Reply

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