PHP Tutorials

PHP Operators Tutorial: Arithmetic, Logic & More

Learn how PHP operators work, from arithmetic and assignment to comparison, logical, and string operators, with clear examples you can use right away.

Serhat ALPASLAN5 min read

Contents

PHP

Operators are symbols with a special meaning in PHP, and they specify the kinds of operations you can perform. They are usually used for mathematical operations.

In PHP, operators are divided into 8 different types:

  • Array operators
  • Arithmetic operators
  • Assignment operators
  • Conditional assignment operators
  • Comparison operators
  • Increment operators
  • Logical operators
  • String operators

1. Array Operators

We generally use array operators to compare arrays. In the table below you can see which symbols these comparisons are made with. However, since arrays are a somewhat broader topic, we’ll cover them in a separate lesson.

Operator Name Description
+ Union The union of two arrays
== Equal Returns TRUE if the two arrays have the same values.
=== Identical Returns TRUE if both arrays have the same value types and values.
!= Not Equal Returns TRUE if the two arrays do not have the same values.
<> Not Equal Returns TRUE if the two arrays do not have the same values.
!== Not Identical Returns TRUE if the two arrays do not have the same values in the same order and of the same type

2. Arithmetic Operators

In PHP, mathematical operators are used to perform mathematical operations. The table below lists the arithmetic operators.

Operator Name Description
+ Add Adds the right-hand value to the left-hand one
– Subtract Subtracts the right-hand value from the left-hand one
* Multiply Multiplies the left-hand value by the right-hand value
/ Divide Returns the quotient of the left-hand and right-hand values
% Modulo Takes the modulus of the left-hand value by the right-hand value and returns the result
** Exponentiation Returns the left-hand value raised to the power of the right-hand value

3. Assignment Operators

In PHP, we use assignment operators to assign a value to your variables and to work with numeric values. You can see the list of operators in the table below.

Assignment Equivalent Form Description
= a = b the value of the variable on the right is assigned to the variable on the left.
+= a = a + b the sum of the variables on the right is assigned to the variable on the left.
-= a = a – b the result of the subtraction on the right is assigned to the variable on the left.
*= a = a * b the result of the multiplication on the right is assigned to the variable on the left.
/= a = a / b the result of the division on the right is assigned to the variable on the left.
%= a = a % b the remainder from dividing the left-hand variable by the right-hand one is assigned to the left-hand variable.

4. Conditional Assignment Operators

In PHP you can assign values depending on the result of certain conditions. We’ll look at this topic in more detail in later lessons, but you can take a look to get started.

Operator Name Example Description
? : Ternary $a = condition ? value1 : value2 If the condition is true, the $a variable returns value1.
If the condition is not true, the $a variable returns value2.
? ? Null coalescing $a = value1 ?? value2 If value1 is not NULL, the value of value1 is assigned to the $a variable
If value1 is NULL, the value of value2 is assigned to the $a variable

5. Comparison Operators

Operator Name Description
== Equal Returns TRUE if the right-hand value is equal to the left-hand value
=== Identical Returns TRUE if the right-hand value and type are equal to the left-hand value and type
!= Not Equal Returns TRUE if the right-hand value and type are not equal to the left-hand value and type
<> Not Equal Returns TRUE if the right-hand value and type are not equal to the left-hand value and type
!== Not Identical Returns TRUE if the right-hand value and type are not identical to the left-hand value and type
> Greater Than Returns TRUE if the left-hand value is greater than the right-hand value
< Less Than Returns TRUE if the left-hand value is less than the right-hand value
>= Greater Than or Equal To Returns TRUE if the left-hand value is greater than or equal to the right-hand value
<= Less Than or Equal To Returns TRUE if the left-hand value is less than or equal to the right-hand value

6. Increment Operators

In PHP, increment operators are used to systematically increase a value or, conversely, decrease it.

Operator Name Description
++$a Pre-increment Increments by one and returns the $a variable.
$a++ Post-increment Returns the value of $a, then increments it by one
–$a Pre-decrement Decrements by one and returns the $a variable.
$a– Post-decrement Returns the value of $a, then decrements it by one.

7. Logical Operators

PHP logical operators are used to combine values within certain conditions.

Operator Name Description
and And If both the left-hand and right-hand values are true, the result is true (TRUE)
or Or If either the left-hand or the right-hand value is true, the result is true (TRUE)
xor Exclusive Or If the left-hand variable or the right-hand variable is true, the result is true; if both are true, the result is false (false)
&& And If both the left-hand and right-hand values are true, the result is true (TRUE)
|| Or If either the left-hand or the right-hand value is true, the result is true (TRUE)
! Not Converts a value that returns false into TRUE, or a value that returns FALSE into TRUE.

8. String Operators

String operators are operators that bring strings together, that is, concatenate them.

Operator Name Description
. Concatenation Concatenates one string with another string.
.= Concatenation and assignment Concatenates one string with another string and assigns the result

 

Share it if it helped

Write a comment