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

3.12     Operators

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

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!





<< 3.11.1 Pre-set constants   3.12.1 Shorthand unary operators >>
Table of Contents
Want to see this stuff in print? PHP in a Nutshell takes the core topics covered here, adds in thousands of edits from the editorial team and myself, and combines them to make an unbeatable reference for PHP programmers at all levels.



My latest book has hundreds more tips on how to use PHP, Apache, and MySQL, plus Perl, Python, shell scripts, performance tuning, and more!



Top-right shadow
 
Bottom-left shadow Bottom shadow

Comments from other readers
A PHP User - 16 Oct 2008

I love the Google ads showing up on this page:

"Machine operator jobs."

"Need a crane operator?"


I now find myself contemplating a career change!

Henry - 16 Oct 2008

If you wonder why they're called the 'Pulp Fiction' operators;

Watch the scene from the movie Pulp Fiction containing 'Jack Rabbit Slims Twist Contest' and watch they're dance moves..

A PHP User - 16 Oct 2008

===, or absolutly equal means this:

-5 === 5
93 === -93
7849874 = -7849874


All of those are true.

A PHP User - 16 Oct 2008

I LOVE KITTENS
they are sooo cute :) and i have a big cat :)
so ill be using brackets all the time :)
thanks for stressing that

A PHP User - 16 Oct 2008

" " === Binary. Tests *absolute* equality between operand one and operands two "

I didn`t get it. Can someone explain to me?""


you could have a string 5
and you could have an integer 5
so they are not absolutely equal
cause they are not the same type :)
(i think :p )

Siam - 16 Oct 2008

hahaha, the "god kills a kitten" part really drove that point home, thanks. :)

A PHP User - 16 Oct 2008

" === Binary. Tests *absolute* equality between operand one and operands two "

I didn`t get it. Can someone explain to me?

A PHP User - 16 Oct 2008

Here are the terms in order of online popularity (from what I've read):
() parentheses, round brackets
[] brackets, square brackets
{} braces, curly braces, curly brackets
<> angle brackets, pointy brackets

Buzzard_Mongrel - 16 Oct 2008

Pulp Fiction ?

A PHP User - 16 Oct 2008

For E
(5 + 5) - (5 + 5) = 0
5 + (5 - 5) + 5 = 10

E - 16 Oct 2008

You might want to call it order of operations as well as precedence, and maybe give a better example of where it matters. You are right in that 5+5-5+5 can be intepreted in a few ways, but the result is the same, 10.

A PHP User - 16 Oct 2008

Just the language barrier at work here.
http://www.phrases.org.uk/bulletin_board/21/messages/232.html

A PHP User - 16 Oct 2008

Parenthesis..

A PHP User - 16 Oct 2008

Is 'brackets' really the right word for the '()' characters? I thought brackets were this '[]' and these '()' were called ellipses or something?



Add comment
Please note that by posting a comment here you are committing it to the public domain. This is important so that others can make use of your code themselves, and also so that I can incorporate helpful notes directly into the main text. Comments are limited to 2000 characters in length.

If you are reporting an error in the content, please tell me directly.

Your name/email address:
Your comment:
 
Now, in order to verify that you're a real person, please answer this simple question: what is eight plus six?
The answer is:
(please write in
numbers, eg 19)


Top-right shadow
 
Bottom-left shadow Bottom shadow