Php operators are grouped in some sections, we gave the common operators in our previous article;

  • Assignment
  • Arithmetic
  • Comparison
  • Logical
  • String
  • Bitwise
  • Error control
  • Execution
  • Incrementing / decrementing
  • Array
  • Type

What is an operator?
If you want to change a variables value, multiply two numbers, compare two variables you use operators. Operators do operations on variables and sometimes you use them in your functions also.

Assignment Operators
They set the left operands value from its right. If you assign a value to a varible you use = (equals) operator. $variable = 1453; It means assign 1453 to variable.

Arithmetic Operators
They are used for arithmetic operations like adding,subtracting,modulus and multiplying.

-$a Negation Opposite of $a.
$a + $b Addition Sum of $a
and $b.
$a – $b Subtraction Difference of $a and $b.
$a * $b Multiplication Product of $a and $b.
$a / $b Division Quotient of $a and $b.
$a % $b Modulus Remainder of $a divided by $b.
$variable = (((3+7)*9+9)*10)%10;

$variable’s value is 0

$variable = 97%7;

$variable is 6

Comparison Operators
Used for comparing two values, they do not assign anything just compare the values. You may use comparison operators on different types of variables, like comparing string and integer variables, since php is a loosely typed language php does not give you any error, but you should be careful about comparing two variables. If you just want to check if a variable has “FALSE” boolean value you may try to use ($c == false), but if $c is 0 then comparison will be true but that s not what you expect! Ok, no problem php has a solution for this you may always try === for checking for variable type and value. Comparison operators are used everywhere in your code, if you are new to programming just learn them very well.

$a == $b Equal TRUE if $a is equal to $b.
$a === $b Identical TRUE if $a is equal to $b, and they are of the same type. (introduced in PHP 4)
$a != $b Not equal TRUE if $a is not equal to $b.
$a <> $b Not equal TRUE if $a is not equal to $b.
$a !== $b Not identical TRUE if $a is not equal to $b, or they are not of the same type. (introduced in PHP 4)
$a < $b Less than TRUE if $a is strictly less than $b.
$a > $b Greater than TRUE if $a is strictly greater than $b.
$a <= $b Less than or equal to TRUE if $a is less than or equal to $b.
$a >= $b Greater than or equal to TRUE if $a is greater than or equal to $b.

Logical Operators
We use them for logical operations like xor,and, not,or, they return boolean values

$a and $b And TRUE if both $a and $b are TRUE.
$a or $b Or TRUE if either $a or $b is TRUE.
$a xor $b Xor TRUE if either $a or $b is TRUE, but not both.
! $a Not TRUE if $a is not TRUE.
$a && $b And TRUE if both $a and $b are TRUE.
$a || $b Or TRUE if either $a or $b is TRUE.

String Operators
Php has two string operators one of them is string concatenation operator and the other one is concatenating assignment operator.
String concatenating operator is . , and used for concatenating two strings.

$string_me    = 'Izmir';
$string          = 'Istanbul';
$string_new   = 'Eskisehir';
$string_concat = $string_me.$string.$string_new; //concats,adds, strings to each other $string_concat is 'IzmirIstanbulEskisehir'

String concatenating assignment operator is .= , it appends argument on the right to the argument left.

$string = 'C++'; //$string value is 'C++'
$string .= ' PHP'; // PHP is appended to $string , new value of the $string is 'C++ PHP'

Bitwise operators
Bitwise operators are used to evaluate and manipulate of specific bits within an integer. Multiplying by two and dividing by two can be made by bit shifting in Php with bitwise operators.

$a & $b And Bits that are set in both $a and $b are set.
$a | $b Or (inclusive or) Bits that are set in either $a or $b are set.
$a ^ $b Xor (exclusive or) Bits that are set in $a or $b but not both are set.
~ $a Not Bits that are set in $a are not set, and vice versa.
$a << $b Shift left Shift the bits of $a $b steps to the left (each step means “multiply by two”)
$a >> $b Shift right Shift the bits of $a $b steps to the right (each step means “divide by two”)

Error Control Operators

@ is the php error control operator which is used to ignore any error messages in expressions. If it is used in an expression (we put it inf front of expressions) error messages caused by expression will not be showed. I dont suggest this operator, dont try to use it. Dont try to hide errors or notices with @ operator. Try to solve your problems and then use error_reporting function or php.ini file to set your error reporting level.

$variable = @$_SESSION[$key]; //will not show you a notice if the index $key does not exist

Execution operators

Php uses backticks “ as execution operators, you may run any command by using execution operators. It is dangerous to use backticks to run commands on your system, be careful about backticks.

$showme = `ls /var/www`;
echo $showme;

Incrementing / decrementing operators

++$a Pre-increment Increments $a by one, then returns $a.
$a++ Post-increment Returns $a, then increments $a by one.
–$a Pre-decrement Decrements $a by one, then returns $a.
$a– Post-decrement Returns $a, then decrements $a by one.

Array operators

$a + $b Union Union of $a and $b.
$a == $b Equality TRUE if $a and $b have the same key/value pairs.
$a === $b Identity TRUE if $a and $b have the same key/value pairs in the same order and of the same types.
$a != $b Inequality TRUE if $a is not equal to $b.
$a <> $b Inequality TRUE if $a is not equal to $b.
$a !== $b Non-identity TRUE if $a is not identical to $b.

Type operators
İnstance of is used as type operator, if we want to know if an object is instantiated from a certain class we use instanceof.

class Aircraft
{
}
class Airbus extends Aircraft
{
}

class Boeing extends Aircraft
{
}

$airbus = new Airbus();
$boeing = new Boeing();

if($airbus instanceof Airbus)
{
        echo "airbus object is instance of the Airbus class\n";
}
else
{
        echo "airbus object is not instance of Airbus class";
}

if($boeing instanceof Airbus)
{
        echo "boeing object is instance of Airbus class";
}
else
{
        echo "boeing object is not instance of Airbus class\n";
}

Output:
airbus object is instance of the Airbus class
boeing object is not instance of Airbus class

Php has three types of operators

  • Unary operators : They operate on one value, like !,++,–
  • Binary operators : They operate on two value, like assignment, increment and assignment, comparison, multiplying
  • Tertiary operator : ?: It is used for comparison, it is used like in here ( $compare_me == $compare_me_second ) ? $we_are_same : $no_we_are_not_same; If $compare_me and $compare_me_second are same it returns $compare_me