Home Articles Portfolio References Contact

Articles » Unary, Binary, and Ternary Operators

Written by: Michael Bookmark and Share

An operator is a special character or combination of characters that operates on variables.  There's 3 types of operators in PHP: unary, binary and ternary and they can be used to manipulate a variable with up to 3 arguments at a time.  This article is not intended to discuss the meaning and usage of each operator in PHP, but rather to explain the differences between these types of operators and give examples of what each one is.

Unary Operators

Unary operators are operators that only deal with one argument (which is generally a single variable).  In PHP, there's only a few of these as shown in Table 1:

Operator Use
! negation
++ increment by 1
-- decrement by 1
Table 1 - Unary Operators In PHP

Below are some examples of these operators being used.

// negation
$bool = true;
$bool = !$bool;   // $bool will now be false

$k = 0;           // initializes the value of k to 0

// increment by 1
$k++;             // increments k by 1
echo "k = ".$k;   // outputs: k = 1

// decrement by 1
$k--;             // decrements k by 1
echo "k = ".$k;   // outputs: k = 0

As you can see, each operator deals with only one variable.  In negation, only the variable (or expression) immediately following the exclamation mark (!) is negated.  It doesn't change the variable, but instead changes the value of the expression.  For example, when a variable with true value is negated, it will now be perceived as false.  If a variable has the value of 0 and it is negated, the new expression value is 1.

Each of the latter two operators is short for their respective binary operators, as will be discussed in the next section.  These are shorthand and only need one variable.  Unlike the negation operator, these operators change the value of the variable they are used with.

Binary Operators

Binary operators are operators that deal with two arguments, both generally being either variables or constants.  Table 2 shows some examples of binary operators found in PHP:

Operator Use
+ addition
- subtraction
* multiplication
/ division
% remainder division
= assignment
== boolean equality comparision
> boolean greater than
< boolean less than
&& boolean AND
|| boolean OR
Table 2 - Binary Operators In PHP

These operators are the most straight forward type of operators in PHP.  They are very easy to understand because we have seen them all our lives in school.  For example, take a look at the example code below:

$a = 5;
$b = 6;
$c = $a + b;  // $c is now 11

There are actually 4 uses of binary operators.  The first one is the assignment operator which is used to give a certain value to a variable.  It needs two arguments, one to the left and one to the right.  The one to the left must be a variable.  It doesn't make any sense to use a constant there because you can't assign a constant a value other than what it already is.  To the right of the assignment operator can be a variable, constant or expression.  The first two uses of this operator deal strictly with one variable and one constant.  Easy enough.  The 3rd line uses a variable for the left argument and an expression for the right argument.  Even though there is more than variable to the right, it is still just one argument.  $a + $b is an expression that is used as a single argument here.

Binary operators aren't limited to arithmetical uses either.  The lower half of the table shows several examples of boolean operators found in PHP.  Notice the example below:

if ($a > $b)
  echo "blue";

$a > $b forms an expression than has a boolean variable based on the evaluation of the expression.  Let's say $a = 5 and $b = 6, then $a > $b is false.  Therefore this expression is also false and the code within the if statement will not be executed.

Ternary Operators

Tertiary operators are operators that deal with three arguments which can be anything from a constant to a complete boolean expression.  Off hand, I only know of one ternary operator in PHP which is used to reduce if else statements to a single line.  Observe the example code below:

// Standard if else statements
if ($a > $b)
  echo "blue";
  echo "red";

// Ternary equivalent
echo ($a > $b) ? "blue" : "red";

The characters used in this are the question mark (?) and the colon (:) and it has three arguments:

[CODE]<optional action> <argument 1> ? <argument 2> : <argument 3>[/CODE]

Argument 1 is generally a boolean expression of some sort, meaning that it can be evaluated to either true or false.  If it is evaluated to true, then argument 2 is executed.  If false, then argument 3 is used.  The great thing about this operator is how much is can condense if else code.  We just took 4 lines of code and condensed it down to 1 line.  These operators can also be nested as shown below:

echo ($a > $b) ? ($c > $d) ? "blue" : "red" : "green";

// which is equivalent to
if ($a > $b)  {
  if ($c > d)
    echo "blue";
    echo "red";
  echo "green";
That time we took 8 lines down to one.  However, as the complexity of the if else statement increases, the readability of the ternary equivalent decreases, so this operator must be used with disgression.

This concludes the basic overview of the three types of operators in PHP: unary, binary and ternary.  Please let comments below to let me know what you thought!