[PHP] Operators

There are a few different types of operators in PHP and I will cover the most commonly used ones.

Arithmetic Operators

These are list basic maths. The 4 most common are:

Addition

Sum of $a and $b.

<?php
$a 
4;
$b 3;
echo 
$a $b;
?>

Outputs:

7

Subtraction

Difference of $a and $b.

<?php
$a 
4;
$b 3;
echo 
$a $b;
?>

Outputs:

1

Multiplication

Product of $a and $b.

<?php
$a 
4;
$b 3;
echo 
$a $b;
?>

Outputs:

12

Division

Quotient of $a and $b.

<?php
$a 
4;
$b 3;
echo 
$a $b;
?>

Outputs:

1.33333333333

Assignment

An assignment operator assigns a value to a variable name.

<?php
$a 
= ($b 10) + 5;
?>

With this $a equals 15 and $b is set to 10.

<?php
$a 
10;
$a += 5;
$b "hello ";
$b .= "world!";
?>

$a is initially set to 10, then is changed to 15. $b is initially set to 'hello ' then 'hello world!' by using the period before the equals (.=).

Comparison

Comparison operators compare 2 values. These are often used in if statements, for example: if $a equals $b then do this.

$a == $b

Returns TRUE if $a is equal to $b.

<?php
$a 
3;
$b 3;
$c 2;
var_dump($a == $b); // 3 == 3 is true
var_dump($b == $c); // 3 == 2 is false
var_dump(== "s"); // 0 == 0 is true
var_dump("1" == "01"); // 1 == 1 is true
var_dump("1" == "1e0"); // 1 == 1 is true
?>

$a === $b

Returns TRUE if $a is identical to $b.

<?php
$a 
3;
$b 3;
$c 2;
var_dump($a === $b); // 3 === 3 is true
var_dump($b === $c); // 3 === 2 is false
var_dump(=== "s"); // 0 === 0 is false
var_dump("1" === "01"); // 1 === 1 is false
var_dump("1" === "1e0"); // 1 === 1 is false
?>

$a != $b, $a <> $b

Returns TRUE if $a is not equal to $b.

<?php
$a 
3;
$b 3;
$c 2;
var_dump($a != $b); // 3 != 3 is false
var_dump($b <> $c); // 3 <> 2 is true
var_dump(!= "s"); // 0 != 0 is false
var_dump("1" != "01"); // 1 !=1 is false
var_dump("1" <> "1e0"); // 1 <> 1 is false
?>

$a !== $b

Returns TRUE if $a is not identical to $b.

<?php
$a 
3;
$b 3;
$c 2;
var_dump($a !== $b); // 3 !== 3 is false
var_dump($b !== $c); // 3 !== 2 is true
var_dump(!== "s"); // 0 !== 0 is true
var_dump("1" !== "01"); // 1 !== 1 is true
var_dump("1" !== "1e0"); // 1 !== 1 is true
?>

$a < $b

Returns TRUE if $a is less than $b.

<?php
$a 
3;
$b 3;
$c 2;
var_dump($a $b); // 3 < 3 is false
var_dump($b $c); // 3 < 2 is false
var_dump("s"); // 0 < 0 is false
var_dump("1" "01"); // 1 < 1 is false
var_dump("1" "1e0"); // 1 < 1 is false
?>

$a > $b

Returns TRUE if $a is greater than $b.

<?php
$a 
3;
$b 3;
$c 2;
var_dump($a $b); // 3 > 3 is true
var_dump($b $c); // 3 > 2 is true
var_dump("s"); // 0 > 0 is false
var_dump("1" "01"); // 1 > 1 is false
var_dump("1" "1e0"); // 1 > 1 is false
?>

$a <= $b

Returns TRUE if $a is less than or equal to $b.

<?php
$a 
3;
$b 3;
$c 2;
var_dump($a <= $b); // 3 <= 3 is true
var_dump($b <= $c); // 3 <= 2 is false
var_dump(<= "s"); // 0 <= 0 is true
var_dump("1" <= "01"); // 1 <= 1 is true
var_dump("1" <= "1e0"); // 1 <= 1 is true
?>

$a >= $b

Returns TRUE if $a is greater than or equal to $b.

<?php
$a 
3;
$b 3;
$c 2;
var_dump($a >= $b); // 3 >= 3 is true
var_dump($b >= $c); // 3 >= 2 is true
var_dump(>= "s"); // 0 >= 0 is true
var_dump("1" >= "01"); // 1 >= 1 is true
var_dump("1" >= "1e0"); // 1 >= 1 is true
?>

Incrementing/Decrementing

Incrementing/Decrementing operators come in 2 forms, pre- and post-increment and decrement.

<?php
echo "Postincrement\n";
$a 5;
echo 
"Should be 5: " $a++ . "\n";
echo 
"Should be 6: " $a "\n";

echo 
"Preincrement\n";
$a 5;
echo 
"Should be 6: " . ++$a "\n";
echo 
"Should be 6: " $a "\n";

echo 
"Postdecrement\n";
$a 5;
echo 
"Should be 5: " $a-- . "\n";
echo 
"Should be 4: " $a "\n";

echo 
"Predecrement\n";
$a 5;
echo 
"Should be 4: " . --$a "\n";
echo 
"Should be 4: " $a "\n";
?>

Outputs:

Postincrement
Should be 5: 5
Should be 6: 6
Preincrement
Should be 6: 6
Should be 6: 6
Postdecrement
Should be 5: 5
Should be 4: 4
Predecrement
Should be 4: 4
Should be 4: 4

Logical

Logical operators return TRUE when variables match given the operator.

$a and $b, $a && $b

<?php
$a 
3;
$b 3;
$c 0;
var_dump($a and $b); // Returns true
var_dump($a and $c); // Returns false
var_dump($b && $c); // Returns false
?>

$a or $b, $a || $b

<?php
$a 
3;
$b 3;
$c 0;
var_dump($a or $b); // Returns true
var_dump($a or $c); // Returns true
var_dump($b || $c); // Returns true
?>

$a xor $b

<?php
$a 
3;
$b 3;
$c 0;
var_dump($a xor $b); // Returns false
var_dump($a xor $c); // Returns true
var_dump($b xor $c); // Returns true
?>

!$a

<?php
$a 
3;
$b 3;
$c 0;
var_dump(!$a); // Returns false
var_dump(!$b); // Returns false
var_dump(!$c); // Returns true
?>

String

String operators are the same as some of the assignment operators.

<?php
$a 
"Hello ";
$b $a "World!"// now $b contains "Hello World!"

$a "Hello ";
$a .= "World!"// now $a contains "Hello World!"
?>

The first use the concatenation operator (period) to concatenate both strings together. The second uses the concatenation assignment operator which appends the 2nd argument to the first.

For more information about operators in PHP, visit the PHP.net website which lists more operators and their uses.