How to use PHP operators

PHP operators provide simple and efficient methods for data manipulation. Due to their compact form, the code can often be made more understandable and intuitive.

What are PHP operators?

PHP operators are special symbols or strings for operations on PHP variables and values. They’re used to manipulate data, check conditions, and perform mathematical operations. They are often included in PHP functions or PHP classes. But operators also perform essential tasks in complex applications. For example, you can use PHP to retrieve information from a MySQL database and compare or link data using operators.

Tip

With Deploy Now from IONOS you can easily build and deploy sites. Auto-scaling capabilities ensure your applications always deliver the performance they need, without manual intervention. This saves time and resources, so you can focus on developing your projects.

What types of PHP operators are there?

PHP operators perform many operations, including calculations, value assignments, comparisons, and logical expressions.

Here are some of the main PHP operator types:

  • Arithmetic operators
  • Assignment operators
  • Comparison operators
  • Increment and decrement operators
  • Logical operators
  • String operators
  • Array operators
  • Conditional operators

Arithmetic operators

Arithmetic operators are used to perform mathematical calculations.

Operator Name Example Result
+ Addition $x + $y Sum of $x and $y
- Subtraction $x - $y Difference between $x and $y
* Multiplication $x* $y Product from $x to $y
/ Division $x / $y Quotient of $x and $y
% Modulus $x % $y Rest of $x divided by $y
** Exponentiation $x** $y Result of raising $x to the $y’th power

Assignment operators

Assignment operators like =, +=, -=, *=, /= are used to assign values to variables and update variable values.

Assignment Equivalent to Description
x = y x = y The value of y is assigned to x
x += y x = x + y Addition
x -= y x = x - y Subtraction
x *= y x = x* y Multiplication
x /= y x = x / y Division
x %= y x = x % y Modulus

Comparison operators

These operators compare values and evaluate conditions.

Operator Name Example Result
== Equal $x == $y True, when $x equals $y
=== Identical $x === $y True, when $x and $y are identical and the same type
!= Not equal $x != $y True, when $x does not equal $y
<> Not equal $x <> $y True, when $x does not equal $y
!== Not identical $x !== $y True, when $x does not equal $y and are not the same type
> Greater than $x > $y True, when $x is larger than $y
< Less than $x < $y True, when $x is smaller than $y
>= Less than or equal to $x >= $y True, when $x is larger or equal to $y
<= Greater than or equal to $x <= $y True, when $x is smaller or equal to $y
<=> Spaceship $x <=> $y Integer, which is smaller, equal to, or larger than 0, when $x is smaller than, equal to, or larger than $y
IONOS Object Storage
Secure, affordable storage

Cost-effective, scalable storage that integrates into your application scenarios. Protect your data with highly secure servers and individual access control.

Increment and decrement operators

Incrementing or decrementing increases or decreases the value of variables.

Operator Name Description
++$x Pre-increment Increases $x by one and returns $x
$x++ Post-increment Returns $x and then increases $x by one
--$x Pre-decrement Decreases $x by one and returns $x
$x-- Post-decrement Returns $x, then decreases $x by one

Logical operators

Logical operators are used in PHP to create logical expressions and to link or negate conditions.

Operator Name Example Result
and And $x and $y True, when $x and $y are true
or Or $x or $y True, when $x or $y are true
xor Xor $x xor $y True, if either $x or $y are true, but not both
&& And $x && $y True, when $x and $y are true
` ` Or
! Not !$x True, when $x is not true

String operators

These operators work with strings.

Operator Name Example Result
. Concatenation $txt1 . $txt2 Connects $txt1 and $txt2
.= Linking assignment operator $txt1 .= $txt2 Links $txt2 to $txt1

Array operators

There are special PHP operators to link arrays in PHP or to add values in arrays.

Operator Name Example Result
+ Union $x + $y Unifies $x and $y
== Equality $x == $y True, when $x und $y Have the same key/value pairs
=== Identity $x === $y True, when $x and $y have the same key/value pairs, are in the same order, and are the same type
!= Inequality $x != $y True, when $x does not equal $y
<> Inequality $x <> $y True, when $x does not equal $y
!== Non-identity $x !== $y True, when $x and $y are not identical

Conditional operators

These operators assign values based on different conditions.

Operator Name Example Result
?: Ternary operator $x = expr1 ? expr2 : expr3 Returns the value of $x; If expr1 is true, $x is equal to expr2, otherwise $x is equal to expr3.
?? Zero coherant operator $x = expr1 ?? expr2 Returns the value of $x; The value of $x is expr1, when expr1 is available and is not NULL, otherwise $x is equal to expr2.
Tip

You’ll find essential PHP programming concepts in our PHP tutorial. We also recommend checking out our articles on PHP vs Python and PHP vs JavaScript for an even better understanding of PHP.

IONOS Developer API
Manage your hosting products through our powerful API
  • DNS management
  • Easy SSL admin
  • API documentation

Example of PHP operators in practice

PHP operators can be used in many ways and in different parts of your code.

Operators in control structures

In the following example, we’ll implement concatenation and comparison operators for conditional statements.

$age = 25;
$legalAge = 18;
if ($age > $legalAge) {
echo "Your age is: " . $age . " You are old enough to vote.";
} else {
echo "Your age is: " . $age . " You are not old enough to vote.";
}
php

Here, we’re comparing the age ($age) with the legal age ($legalAge) for the right to vote. The comparison operator (>) checks if $age is greater than $legalAge. Depending on whether the condition is true or not, we’ll see whether the person is old enough to choose and connect the age to the concatenation operator. Since $age is greater than $legalAge, the condition is true.

Was this article helpful?
Page top