PHP operators provide simple and efficient methods for data ma­nip­u­la­tion. Due to their compact form, the code can often be made more un­der­stand­able and intuitive.

What are PHP operators?

PHP operators are special symbols or strings for op­er­a­tions on PHP variables and values. They’re used to ma­nip­u­late data, check con­di­tions, and perform math­em­at­ic­al op­er­a­tions. They are often included in PHP functions or PHP classes. But operators also perform essential tasks in complex ap­plic­a­tions. For example, you can use PHP to retrieve in­form­a­tion 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 cap­ab­il­it­ies ensure your ap­plic­a­tions always deliver the per­form­ance they need, without manual in­ter­ven­tion. This saves time and resources, so you can focus on de­vel­op­ing your projects.

What types of PHP operators are there?

PHP operators perform many op­er­a­tions, including cal­cu­la­tions, value as­sign­ments, com­par­is­ons, and logical ex­pres­sions.

Here are some of the main PHP operator types:

  • Arith­met­ic operators
  • As­sign­ment operators
  • Com­par­is­on operators
  • Increment and decrement operators
  • Logical operators
  • String operators
  • Array operators
  • Con­di­tion­al operators

Arith­met­ic operators

Arith­met­ic operators are used to perform math­em­at­ic­al cal­cu­la­tions.

Operator Name Example Result
+ Addition $x + $y Sum of $x and $y
- Sub­trac­tion $x - $y Dif­fer­ence between $x and $y
* Mul­ti­plic­a­tion $x* $y Product from $x to $y
/ Division $x / $y Quotient of $x and $y
% Modulus $x % $y Rest of $x divided by $y
** Ex­po­nen­ti­ation $x** $y Result of raising $x to the $y’th power

As­sign­ment operators

As­sign­ment operators like =, +=, -=, *=, /= are used to assign values to variables and update variable values.

As­sign­ment Equi­val­ent to De­scrip­tion
x = y x = y The value of y is assigned to x
x += y x = x + y Addition
x -= y x = x - y Sub­trac­tion
x *= y x = x* y Mul­ti­plic­a­tion
x /= y x = x / y Division
x %= y x = x % y Modulus

Com­par­is­on operators

These operators compare values and evaluate con­di­tions.

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 Cloud Object Storage
Cloud storage at an un­beat­able price

Cost-effective, scalable storage that in­teg­rates into your ap­plic­a­tion scenarios. Protect your data with highly secure servers and in­di­vidu­al access control.

Increment and decrement operators

In­cre­ment­ing or decre­ment­ing increases or decreases the value of variables.

Operator Name De­scrip­tion
++$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 ex­pres­sions and to link or negate con­di­tions.

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
. Con­cat­en­a­tion $txt1 . $txt2 Connects $txt1 and $txt2
.= Linking as­sign­ment 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
!= In­equal­ity $x != $y True, when $x does not equal $y
<> In­equal­ity $x <> $y True, when $x does not equal $y
!== Non-identity $x !== $y True, when $x and $y are not identical

Con­di­tion­al operators

These operators assign values based on different con­di­tions.

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 pro­gram­ming concepts in our PHP tutorial. We also recommend checking out our articles on PHP vs Python and PHP vs JavaS­cript for an even better un­der­stand­ing of PHP.

IONOS Developer API
Manage your hosting products through our powerful API
  • DNS man­age­ment
  • Easy SSL admin
  • API doc­u­ment­a­tion

Example of PHP operators in practice

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

Operators in control struc­tures

In the following example, we’ll implement con­cat­en­a­tion and com­par­is­on operators for con­di­tion­al state­ments.

$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 com­par­is­on 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 con­cat­en­a­tion operator. Since $age is greater than $legalAge, the condition is true.

Go to Main Menu