Operators
Assignment Operator
Concatenation (.=)
$address = "Beautiful Street"; $city = "New City"; echo $address . " " . $city; $province = "Great Province"; $address .= " " . $city . " " . $province; echo $address;
Beautiful Street New City
Beautiful Street, New City Great Province
Beautiful Street, New City Great Province
Addition (+=)
$amount = 100; $amount = $amount + 1; $amount += 1; echo $amount;
102
Subtraction (-=)
$amount = 100; $amount = $amount - 1; $amount -= 1; echo $amount;
98
Multiplication (*=)
$amount = 100; $amount = $amount * 2; $amount *= 2; echo $amount;
400
Division (/=)
$amount = 100; $amount = $amount / 2; $amount /= 2; echo $amount;
25
Modulus (%=)
$amount = 100; $amount = $amount % 2; $amount %= 2; echo $amount;
0
Increment Operator
Pre Increment (++)
$amount = 100; echo ++$amount; echo $amount;
101
101
101
Post Increment
$amount = 100; echo $amount++; echo $amount;
100
101
101
Decrement Operator
Pre Decrement (--)
$amount = 100; echo --$amount; echo $amount;
99
99
99
Post Decrement
$amount = 100; echo $amount--; echo $amount;
100
99
99
Expression Operators
Comparison operator (==)
$activeFlag = 1; $active = 1; echo ($activeFlag == $active) ? "Active" : "Inactive"; // always use == for comparison instead of = sign.
Active
Identical operator (===)
$val_1 = 10; $val_2 = "10"; echo ($val_1 == $val_2) ? "true" : "false"; echo ($val_1 === $val_2) ? "true" : "false";
true
false
false
Not Equal To (!=)
$activeFlag = 1; $active = 1; echo ($activeFlag != $active) ? "Active" : "Inactive";
Inactive
Exponential operator (**)
echo (2 ** 3);
8
Logical Operators
And operator (&&)
$min_value = 1; $max_value = 100; $input = 50; echo ($input >= $min_value && $input <= $max_value) ? "Valid input" : "Invalid input";
Valid input
$result = true && false; echo $result ? "true" : "false";
false
And operator (and) (operator = has precedence over operator and)
$result = true and false; echo $result ? "true" : "false";
true
Or Operator (||)
$min_value = 100; $max_value = 200; $input = 150; echo ($input >= $min_value || $input <= $max_value) ? "Valid input" : "Invalid input";
Valid input
Or operator (or) (operator = has precedence over operator or)
$result = false or true; echo $result ? "true" : "false";
false
Bitwise Operators
And operator (&)
$stored_bit = 0; // 0000 $input_bit = 1; // 0001 echo ($stored_bit & $input_bit); // (0000) & (0001) => 0
0
$stored_bit = 2; // 0010 $input_bit = 3; // 0011 echo ($stored_bit & $input_bit); // (0010) & (0011) => 2
2
Or operator (|)
$stored_bit = 0; // 0000 $input_bit = 1; // 0001 echo ($stored_bit | $input_bit); // (0000) | (0001) => 1
1
$stored_bit = 2; // 0010 $input_bit = 3; // 0011 echo ($stored_bit & $input_bit) ; // (0010) | (0011) => 3
3
xor operator (^)
$stored_bit = 2; // 0010 $input_bit = 3; // 0011 echo ($stored_bit ^ $input_bit); // (0010) ^ (0011) => 1 - because 0 ^ 1 => 1, 1 ^ 1 => 0
1
Execution operator (``) backtick
$name = "PHP"; $command = "echo Hello $name"; echo `$command`; // use backtick for executing command.
Hello PHP
Error Control Operator
$amount = 100; @$avg = $amount / 0; // will raise an error but due to @ symbol it will not display the error. echo (isset($php_errormsg)) ? "Error " . $php_errormsg : ""