ovi.ro

ovi.ro

Operators list

Arithmetic Operators

Example Name Result
$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 Modulus Remainder of $a divided by $b.

Assignment Operators

<?php $a = 5; ?>

<?php $a = ($b = 2) + 5;  // $a = 7 ?>

<?php
  $a = 2;
  $a += 3; // sets $a to 5, equivalent to: $a = $a + 5;
  $b = "Hello ";
  $b .= "World!"; // sets $b to "Hello There!", equivalent to  $b = $b . "There!";  ( dot is for contatenation)
?>

 Assignment by Reference

<?php
  $a = 3;
  $b = &$a; // $b is a reference to $a - changes of $a will apply to $b too
  echo $a . $b;   // prints 33

  $a = 4;
  echo $a . $b;   // prints 44

  class C {}

  $o = &new C;  // as of PHP 5 classes are passed by default by reference
              // as of PHP 5.3 this will throw an E_DEPRECATED error
?>

 Bitwise Operators

Format Name Example
$a & $b And 1001 & 0101  = 0001
$a | $b Or (inclusive or) 1001 | 0101 = 1101
$a ^ $b Xor (exclusive or) 1001 ^ 0101  = 0100
~ $a Not ~ 1001 = 0110
$a << $b Shift left 0010 << 1 = 0100
$a >> $b Shift right Shift the bits of $a $b steps to the right (each step means “divide by two”)