Hudzilla.org - the homepage of Paul Hudson PHP and PHP
Contents > Simple variables and operators > Operators Wish List | Report Bug | About Me ]

3.12.1     Shorthand unary operators

NOTE: This is NOT the latest copy of this book; click here for the latest version.

The = operator is special in that you can combine it with other operators. For example, to turn > (greater than) into greater than or equals, add the equals sign to the end - >=. You can add an equals sign to each of the first six operators in the list, forming +=, -=, *=, /=, and .=, and these become unary operators, like this:

<?php
    $somevar
= 5;
    
$somevar += 5; // $somevar is now 10
    
$somevar *= 2; // $somevar is now 20
    
$somevar .= " elephants"; // $somevar is now "20 elephants"
?>

The unary versions of these operators are really just shorthand, but they are a little easier to read also.



<< 3.12 Operators   3.12.2 Comparison operators >>
Table of Contents
Top-right shadow
 
Bottom-left shadow Bottom shadow