PHP

PHP Operators

Operators are used to perform operations on variables and values. PHP have the following operators.

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

Operator Precedence

Precedence refers to the order in which different operations will be executed.

For example, in the expression 1 + 5 * 3, the answer is 16 and not 18 because the multiplication ("*") operator has a higher precedence than the addition ("+") operator. Parentheses may be used to force precedence, if necessary. For instance: (1 + 5) * 3 evaluates to 18.

The following table lists the operators in order of precedence, with the highest-precedence ones at the top. Operators on the same line have equal precedence, in which case associativity decides grouping.

Associativity

Operators

Additional Information

non-associative

clone new

clone and new

left

[

array()

right

**

arithmetic

right

++ -- ~ (int) (float) (string) (array) (object) (bool) @

types and increment/decrement

non-associative

instanceof

types

right

!

logical

left

* / %

arithmetic

left

+ - .

arithmetic and string

left

<< >>

bitwise

non-associative

< <= > >=

comparison

non-associative

== != === !== <> <=>

comparison

left

&

bitwise and references

left

^

bitwise

left

|

bitwise

left

&&

logical

left

||

logical

right

??

comparison

left

? :

ternary

right

= += -= *= **= /= .= %= &= |= ^= <<= >>=

assignment

left

and

logical

left

xor

logical

left

or

logical


Example  of Associativity-


$a = 3 * 3 % 5; // (3 * 3) % 5 = 4

// ternary operator associativity differs from C/C++

$a = true ? 0 : true ? 1 : 2; // (true ? 0 : true) ? 1 : 2 = 2

$a = 1;

$b = 2;

$a = $b += 3; // $a = ($b += 3) -> $a = 5, $b = 5

?>

PHP Arithmetic Operators

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

Example

OperatorName

Result

+$a

Identity (+)

Conversion of $a to int or float as appropriate.

-$a

Negation -

Opposite of $a.

$a + $b

Addition  (+)

Sum of $a and $b.

$a - $b

Subtraction (-)

Difference of $a and $b.

$a * $b

Multiplication (*)

Product of $a and $b.

$a / $b

Division (/)

Quotient of $a and $b.

$a % $b

Modulo (%)

Remainder of $a divided by $b.

$a ** $b

Exponentiation (**)

Result of raising $a to the $b'th power

Assignment Operators

The basic assignment operator is equal to ( = ) . It sets the right side expression value to left operand.

Example-


$a = ($b = 4) + 5; // $a is equal to 9 now, and $b has been set to 4.

?>

Bitwise Operators

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

Bit shifting in PHP is arithmetic. Bits shifted off either end are discarded. Left shifts have zeros shifted in on the right while the sign bit is shifted out on the left, meaning the sign of an operand is not preserved. Right shifts have copies of the sign bit shifted in on the left, meaning the sign of an operand is preserved.

Example

Name

Result

$a & $b

And

Bits that are set in both $a and $b are set.

$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")


Comparison Operators

Comparison operators, as their name implies, allow you to compare two values.

Example

Name

Result

$a == $b

Equal

TRUE if $a is equal to $b after type juggling.

$a === $b

Identical

TRUE if $a is equal to $b, and they are of the same type.

$a != $b

Not equal

TRUE if $a is not equal to $b after type juggling.

$a <> $b

Not equal

TRUE if $a is not equal to $b after type juggling.

$a !== $b

Not identical

TRUE if $a is not equal to $b, or they are not of the same type.

$a < $b

Less than

TRUE if $a is strictly less than $b.

$a > $b

Greater than

TRUE if $a is strictly greater than $b.

$a <= $b

Less than or equal to

TRUE if $a is less than or equal to $b.

$a >= $b

Greater than or equal to

TRUE if $a is greater than or equal to $b.

$a <=> $b

Spaceship

An integer less than, equal to, or greater than zero when $a is respectively less than, equal to, or greater than $b.  It is included in PHP 7.

Incrementing/Decrementing Operators

PHP supports C-style pre- and post-increment and decrement operators.

Example

Name

Effect

++$a

Pre-increment

Increments $a by one, then returns $a.

$a++

Post-increment

Returns $a, then increments $a by one.

--$a

Pre-decrement

Decrements $a by one, then returns $a.

$a--

Post-decrement

Returns $a, then decrements $a by one.


Note: The increment/decrement operators only affect numbers and strings. Arrays, objects and resources are not affected. Decrementing NULL values has no effect too, but incrementing them results in 1.

Example-



  echo "

Postincrement Sample

";


  $a = 5;

  echo "It will print 5: " . $a++ . "
\n";


  echo "It will print 6: " . $a . "
\n";


  

  echo "

Preincrement Sample

";


  $a = 5;

  echo "It will print 6: " . ++$a . "
\n";


  echo "It will print 6: " . $a . "
\n";


  

  echo "

Postdecrement Sample

";


  $a = 5;

  echo "It will print 5: " . $a-- . "
\n";


  echo "It will print 4: " . $a . "
\n";


  

  echo "

Predecrement Sample

";


  $a = 5;

  echo "It will print 4: " . --$a . "
\n";


  echo "It will print 4: " . $a . "
\n";


  ?>


PHP Logical Operators

The PHP logical operators are used to combine conditional statements.

Example

Name

Result

$a and $b

And

TRUE if both $a and $b are TRUE.

$a or $b

Or

TRUE if either $a or $b is TRUE.

$a xor $b

Xor

TRUE if either $a or $b is TRUE, but not both.

! $a

Not

TRUE if $a is not TRUE.

$a && $b

And

TRUE if both $a and $b are TRUE.

$a || $b

Or

TRUE if either $a or $b is TRUE.

PHP String Operators

PHP has two operators that are specially designed for strings.

Operator

Name

Example

Result

 

.

Concatenation

$str1 . $str2

Concatenation of $str1 and $str2

 

.=

Concatenation assignment

$str1 .= $str2

Appends $str2 to $str1

 

Example-



$a = "Welcome";

$b = $a . "PHP!"; // now $b contains "Welcome PHP!"

$a = "Welcome";

$a .= "PHP!";     // now $a contains "Welcome PHP!"

?>

PHP Array Operators

The PHP array operators are used to compare arrays.

Example

Name

Result

$a + $b

Union

Union of $a and $b.

$a == $b

Equality

TRUE if $a and $b have the same key/value pairs.

$a === $b

Identity

TRUE if $a and $b have the same key/value pairs in the same order and of the same types.

$a != $b

Inequality

TRUE if $a is not equal to $b.

$a <> $b

Inequality

TRUE if $a is not equal to $b.

$a !== $b

Non-identity

TRUE if $a is not identical to $b.


Type Operators

instanceof is used to determine whether a PHP variable is an instantiated object of a certain class:


Example-


class MyClass{
}
class NotMyClass{
}
$a = new MyClass;

var_dump($a instanceof MyClass);

var_dump($a instanceof NotMyClass);

?>

The above example will output:

bool(true)

bool(false)



Back to Top