Keyboard shortcuts

Press ← or → to navigate between chapters

Press S or / to search in the book

Press ? to show this help

Press Esc to hide this help

Unary Operator

A unary operator is an operator that operates on a single operand. It performs an operation on the value of the operand. PHP supports several unary operators, including the following:

  • Post increment operator $a++
  • Pre increment operator ++$a
  • Post decrement operator $a--
  • Pre decrement operator --$a
  • Positive operator +$a, which produces the same value
  • Negative operator -$a, which produces the opposite of the value
  • Logical Not operator !$a
  • Bitwise Not operator ~$a
  • Noscream operator @$a

The first four operators change the underlying value and also return the value. The last three operators only return the changed value, leaving the original value unchanged.

<?php

$a = 10;
print -$a;  // -10, $a unchanged
print $a;   // 10
print ++$a; // 10, $a changed
print $a;   // 11

?>

Documentation

See Also