Operators

Operators perform actions on operands - they modify values of input. For example, in the equation "2 + 3", the 2 and the 3 are both operands, and the + is the operator. There are three types of operator: unary, binary, and ternary. Unary operators take just one operand, binary operators take two operands, and ternary take three. As you can see, the + operator (used to add numerical values) is a binary operator - it takes two variables as input.

There are a large number of operators available for your use, of which perhaps about fourteen are important to remember.

First, the most important operators. Do not worry about remembering them all straight away - there will be examples to help you along afterwards:

+

Binary. Adds operand one to operand two

-

Binary. Subtracts operand two from operand one

*

Binary. Multiples operand one by operand two

/

Binary. Divides operand one by operand two

.

Binary. Appends string operand two to operand one

!

Unary. Acts as an inverter (true becomes false and false becomes true)

++ --

Unary. Increments (++) or decrements (--) a variable

=

Binary. Assigns operand two to operand one

==

Binary. Tests equality between operand one and operands two

===

Binary. Tests absolute equality between operand one and operands two

< >

Binary. Less than and greater than, although also called the "Pulp Fiction" operators (if you don't get it, don't fret!)

&&

Binary. "Logical AND"; tests whether two sets of conditions are true

||

Binary. "Logical OR"; tests whether either of two sets of conditions are true

Here are some examples of most of these operators in action:

<?php
    $somevar = 5 + 5; // 10
    $somevar = 5 - 5; // 0
    $somevar = 5 + 5 - (5 + 5); // 0
    $somevar = 5 * 5; // 25
    $somevar = 10 * 5 - 5; // 45
    $somevar = $somevar . "appended to end";
    $somevar = false;
    $somevar = !$somevar; // $somevar is now set to true
    $somevar = 5;
    $somevar++; // $somevar is now 6
    $somevar--; // $somevar is now 5 again
    ++$somevar; // $somevar is 6
?>

The third expression there uses brackets to separate what is going on. This is important, as the equation "5 + 5 - 5 + 5" can be taken in more than one way, such as "5 + (5 - 5) + 5), which is 10. There are some equations, such as equation five, where brackets are not needed. There, "10 * 5 - 5" can only be taken to mean "(10 * 5) - 5" because of the mathematical rules of precedence - multiplication is considered more important than subtraction.

Despite each operator having very specific precedence, it is still best to use brackets in order to make your meaning clear. Expressions inside brackets are always evaluated first, and you can use any number of brackets in order to get the expression correct.

Author's Note: If you're rushing through this, you may just have missed an important recommendation: "despite each operator having very specific precedence, it is still best to use brackets in order to make your meaning clear."

I cannot really overstress how important that is - if you don't use brackets, you force readers of your code to have memorised the operator precedence and associativity tables, which is crazy. Every time you don't use brackets in a potentially confusing situation, God kills a kitten!

 

Want to learn PHP 7?

Hacking with PHP has been fully updated for PHP 7, and is now available as a downloadable PDF. Get over 1200 pages of hands-on PHP learning today!

If this was helpful, please take a moment to tell others about Hacking with PHP by tweeting about it!

Next chapter: Shorthand unary operators >>

Previous chapter: Pre-set constants

Jump to:

 

Home: Table of Contents

Copyright ©2015 Paul Hudson. Follow me: @twostraws.