Operators in PHP with types and examples

Operators are special symbols or keywords in PHP that carry out specific operations on variables or values. Arithmetic, comparison, logical, assignment, and bitwise operators are all supported by PHP.

Types of operators in PHP

These are the main operators in PHP. Let's explain these operators one by one.

Arithmetic operators in PHP

Arithmetic operators carry out mathematical operations on numbers. The following arithmetic operators are supported by PHP:

Operator Description
+ Adds two values.
- This operator subtracts one value from another.
* Multiplies two values.
/ This operator divides one value by another.
% Returns the remainder of a division operation.

PHP arithmetic operator example

PHP Code
<?php 
   $a = 11;
   $b = 5;
   
   echo $a + $b;
   echo "<BR>";
   
   echo $a - $b;
   echo "<BR>";
   
   echo $a * $b;
   echo "<BR>";
   
   echo $a / $b;
   echo "<BR>";
   
   echo $a % $b;
?>
Output
16
6
55
2.2
1

Comparison operators in PHP

The comparison operator compares two values and returns a Boolean result. The following comparison operators are supported by PHP:

Operator Description
== If both values are equal, this operator returns true or 1.
!= If both values are not equal, this operator returns true.
=== If both values are equal and of the same type, this operator returns true.
!== Returns true if neither value is equal nor of the same type.
> If the left value is greater than the right value, this operator returns true.
< If the left value is less than the right value, this operator returns true.
>= If the left value is greater than or equal to the right value, this operator returns true.
<= If the left value is less than or equal to the right value, this operator returns true.

PHP comparison operators example

PHP Code
<?php 
   $a = 11;
   $b = 5;
   $c = 11;
   
   echo $a == $c;
   echo "<BR>";
   
   echo $a != $b;
   echo "<BR>";
   
   echo $a === $c;
   echo "<BR>";
   
   echo $a !== $b;
   echo "<BR>";
   
   echo $a > $b;
   echo "<BR>";
   
   echo $b < $c;
   echo "<BR>";
   
   echo $a >= $b;
   echo "<BR>";
   
   echo $b <= $c;
?>
Output
1
1
1
1
1
1
1
1

Logical operators in PHP

A Boolean result is produced by combining two or more Boolean expressions using logical operators. The following logical operators are supported by PHP:

Operator Description
&& If both expressions are true, this operator returns true.
|| If at least one expression is true, this operator returns true.
! If the expression is true, this operator returns false, and vice versa.

PHP logical operators example

PHP Code
<?php 
   $a = true;
   $b = false;
   $c = true;
   
   echo $a && $c;
   echo "<BR>";
   
   echo $a || $c;
   echo "<BR>";
   
   echo !$b;
?>
Output
1
1
1

Assignment operators in PHP

Variables can have values assigned to them using assignment operators. The following assignment operators are supported by PHP:

Operator Description
= assigns a variable a value.
+= Adds a value to a variable and then returns the result.
-= The operator subtracts a value from a variable and assigns the result to the variable.
*= A variable is multiplied by a value and the result is assigned to the variable.
/= The result of dividing a variable by a value is assigned to the variable.
%= The remainder of a variable divided by a value is calculated and assigned to the variable.

PHP assignment operators example

PHP Code
<?php 
   $a = 11;
   $b = 5;
   
   $a += $b;
   echo $a;
   echo "<BR>";
   
   $a -= $b;
   echo $a;
   echo "<BR>";
   
   $a *= $b;
   echo $a;
   echo "<BR>";
   
   $a /= $b;
   echo $a;
   echo "<BR>";
   
   $a %= $b;
   echo $a;
?>
Output
16
11
55
11
1

The following statement:

$a += $b;

is equivalent to

$a = $a + $b;

Similarly, the following statement:

$a -= $b;

is equivalent to

$a = $a - $b;

and so on.

Bitwise operators in PHP

Bitwise operators operate on numeric values' binary representations. PHP supports the bitwise operators listed below:

Operator Description
& when both operands are 1, this function returns a 1 in every bit position.
! Returns a 1 in each bit position where either operand has a 1.
^ Returns a 1 in every case where there is only one 1 in the operand.
˜ bits in its operand are inverted.
<< by the number of positions specified by the right operand, shifts the bits of the left operand to the left.
>> by the number of positions specified by the right operand, shifts the bits of the left operand to the right.

PHP bitwise operators example

PHP Code
<?php 
   $a = 10;
   $b = 5;
   
   echo $a & $b;
   echo "<BR>";
   
   echo $a | $b;
   echo "<BR>";
   
   echo $a ^ $b;
   echo "<BR>";
   
   echo ˜$a;
   echo "<BR>";
   
   echo $a << 1;
   echo "<BR>";
   
   echo $a >> 1;
?>
Output
0
15
15
-11
20
5

The binary equivalent of 10 is 1010, 5 is 0101, 0 is 0000, 15 is 1111, -11 is 11110101, and 20 is 10100.

Increment/Decrement operators in PHP

The increment/decrement operators in PHP are used to change a variable's value by one. Since they only operate on one variable, they are categorized as unary operators.

A variable's value is increased by the increment operator (++) by 1 and decreased by the decrement operator (--) by 1. The order in which these operators are used can have an impact on the operation's outcome. These operators can be used either before or after the variable they are operating on.

This type of increment/decrement operator is known as a pre-increment/pre-decrement operator because it is applied before the variable. The variable's value is altered before it is plugged into the expression. Post-increment or post-decrement operators are those that come after the variable they are modifying. Here, the variable's value is changed after it has been used in an expression.

PHP Increment/Decrement operators example

PHP Code
<?php 
   $a = 5;
   echo ++$a;     // increments, then use
   echo "<BR>";
   
   echo $a;
   echo "<BR>";
   
   echo $a++;     // use, then increments
   echo "<BR>";
   
   echo $a;
   echo "<BR>";

   $b = 5;
   echo --$b;     // decrements, then use
   echo "<BR>";
   
   echo $b;
   echo "<BR>";
   
   echo $b--;     //  use, then decrements
   echo "<BR>";
   
   echo $b;
?>
Output
6
6
6
7
4
4
4
3

Conditional operators in PHP

The conditional operator in PHP is a one-line shortcut for writing an if-else statement. The syntax is as follows:

condition ? x : y;

The "condition" will be evaluated first; if it is evaluated as true, then "x" will be returned, otherwise "y" will be returned.

PHP conditional operator example

PHP Code
<?php 
   $a = 5;
   $b = 10;
   
   $c = $a>$b ? "codes" : "cracker";
   echo $c;
   echo "<BR>";
   
   $d = $a<$b ? "codes" : "cracker";
   echo $d;
?>
Output
cracker
codes

The step-by-step explanation of the previous code is:

The code then prints out the values of $c and $d separated by a line break (<BR>).

Since $a is not greater than $b, $c is assigned the value "cracker". And, since $a is less than $b, so $d is assigned the value "codes".

PHP Online Test


« Previous Tutorial Next Tutorial »


Liked this post? Share it!