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.

{| style=”text-align: center” cellpadding=”10” cellspacing=”0” border=”1” |- ! 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.

{| style=”text-align: center” cellpadding=”10” cellspacing=”0” border=”1” |- ! . (period) ! “Hello” . ” ” . “World”

“Hello World”
}

= Assignment Operators =

Assignment operators causes a variable to contain specific data.

{| style=”text-align: center” cellpadding=”10” cellspacing=”0” border=”1” |- ! 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 {| style=”text-align: center” cellpadding=”10” cellspacing=”0” border=”1” |- ! 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 [[PHPBook:Variables_and_Constants#How_do_you_specify_a_variable.27s_data_type.3F|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.

{| style=”text-align: center; border-collapse:collapse; border: 2px solid black” cellpadding=”5” cellspacing=”0” border=”1” |- ! not |  ! !  !(1 == 2) | true |- ! and | && ! style=”padding: 0px; margin: 0; width: 15em” |

1 == 2 && 2 == 3
1 == 1 && 2 == 3
1 == 1 && 2 == 2

|style=”padding: 0px; margin: 0; width: 5em” |

false
false
true

|- ! or | ”’||”’ ! style=”padding: 0px; margin: 0;” |

1 == 2 || 2 == 3
1 == 1 || 2 == 3
1 == 1 || 2 == 2

|style=”padding: 0px; margin: 0;” |

false
true
true

|- ! xor | xor ! style=”padding: 0px; margin: 0;” |

1 == 2 xor 2 == 3
1 == 1 xor 2 == 3
1 == 1 xor 2 == 2

|style=”padding: 0px; margin: 0;” |

false
true
false

|}