Operators are special (usually mathematically-oriented) symbols that tell the PHP interpreter how to manipulate one or more variables.

There are five main classes of operators (sorted by commonality, descending):

  • Arithmetic operators
  • The String concatenation operator
  • Assignment operators
  • Comparison operators
  • Logical operators
Arithmetic Operators

Arithmetic operators manipulate a value, usually integers and floats, using basic mathematical algorithms.

Addition + 4 + 7 11
Subtraction - 10 - 7 3
Multiplication * 3 * 7 21
Increment ++$data $data = 5;

++$data;

6
Decrement —$data $data = 5;

—$data;

4
String Concatenation Operator

The string concatenation operator does what it says: It joins two or more strings together.

. (period) “Hello” . ” ” . “World” “Hello World”
Assignment Operators

Assignment operators causes a variable to contain specific data.

sym expression test output
= $x = 4; echo $x; 4
+= $x = 2;

$x += 4;

echo $x; 6
-= $x = 4;

$x -= 2;

echo $x; 2
.= $x = “Hello “;

$x .= “World”;

echo $x; “Hello World”
*= $x = 4;

$x *= 2;

echo $x; 8
-= $x = 8;

$x /= 2;

echo $x; 4
 %= $x = 23;

$x %= 3;

echo $x; 2
Comparison Operators

Comparison operators help you determine whether a statement is true or false, by comparing two variables to one another in different ways.

The logic operators in PHP are

Equal To == Not Equal To !=
Greater Than > Greater Than or Equal >=
Lesser Than < Lesser Than or Equal <=
Equal To and Same Type === Not Equal To or Different Type !==
Examples

Most of these should be fairly straight forward.

$name = "Ted";

if ($name == "Ted")
{
    echo "Hi, Ted!";
}

$cost = 2.2;
$money = 5.2;

if ($money >= $cost)
{
    echo 'You have enough money!';
}

if ($money < $cost)
{
    echo 'You do not have enough money.';
}

The operators === and !== also compare the data types of the two variables. While not used very often, in some situations (like the [http://www.php.net/manual/en/function.strpos.php strpos function]) they are absolutely necessary.

Consider the following:

function trueOrFalse($condition)
{
    if ($condition) { echo "True!\n"; }
    else { echo "False!\n"; }
}

trueOrFalse(1 == 1);      // Output: True!
trueOrFalse(1 == "1");    // Output: True!
trueOrFalse(1 == true);   // Output: True!
trueOrFalse(1 == "true"); // Output: False!

trueOrFalse(1 === 1);      // Output: True!
trueOrFalse(1 === "1");    // Output: False!
trueOrFalse(1 === true);   // Output: False!
trueOrFalse(1 === "true"); // Output: False!
Logical Operators

Logical operators combine two or more comparison operations.

  • A ”’not”’ expression returns the opposite (true becomes false and vice versa).
  • An ”’and”’ expression is true if both sides are true.
  • An ”’or”’ expression is true if either side is true.
  • An ”’xor”’ expression is true if only one side is true.
not  !  !(1 == 2) true
and &&
1 == 2 && 2 == 3
1 == 1 && 2 == 3
1 == 1 && 2 == 2
false
false
true
or ||
1 == 2 || 2 == 3
1 == 1 || 2 == 3
1 == 1 || 2 == 2
false
true
true
xor xor
1 == 2 xor 2 == 3
1 == 1 xor 2 == 3
1 == 1 xor 2 == 2
false
true
false