PHP Tutorial: Operators

Operators

Operators are used to perform operations on variables and values.

PHP has many operators which are divided into following categories:

  • Arithmetic operators
  • Assignment operators
  • Comparison operators
  • Increment/Decrement operators
  • Logical operators
  • String operators
  • Array operators

 

Arithmetic Operators

The PHP arithmetic operators are used with numeric values to perform common arithmetical operations, such as addition, subtraction, multiplication etc.

Operator

Name

Example

+

Addition

2+4

-

Substraction

6-2

*

Multiplication

5 * 3

/

Division

15 / 3

%

Modulus

43 % 10

 

Example

<?php

$addition = 2 + 4;

$subtraction = 6 - 2;

$multiplication = 5 * 3;

$division = 15 / 3;

$modulus = 5 % 2;

echo "Perform addition: 2 + 4 = ".$addition."<br />";

echo "Perform subtraction: 6 - 2 = ".$subtraction."<br />";

echo "Perform multiplication:  5 * 3 = ".$multiplication."<br />";

echo "Perform division: 15 / 3 = ".$division."<br />";

echo "Perform modulus: 5 % 2 = " . $modulus. ". Modulus is the remainder after the division operation has been performed. 

          In this case it was 5 / 2, which has a remainder of 1.";

?>

Output:

Perform addition: 2 + 4 = 6

Perform subtraction: 6 - 2 = 4

Perform multiplication: 5 * 3 = 15

Perform division: 15 / 3 = 5

Perform modulus: 5 % 2 = 1. Modulus is the remainder after the division operation has been performed. In this case it was 5 / 2, which has a remainder of 1.

Assignment Operators

The PHP assignment operators are used with numeric values to assign a value to a variable.

The basic assignment operator in PHP is "=". It means that the left operand gets set to the value of the assignment expression on the right.

Operator

Name

Example

Equivalent Operation

+=

Plus Equals

$x += 2;

$x = $x + 2;

-=

Minus Equals

$x -= 4;

$x = $x - 4;

*=

Multiply Equals

$x *= 3;

$x = $x * 3;

/=

Divide Equals

$x /= 2;

$x = $x / 2;

%=

Modulo Equals

$x %= 5;

$x = $x % 5;

.=

Concatenate Equals

$my_str.="hello";

$my_str = $my_str . "hello";

 

PHP Comparison Operators

Comparisons are used to check the relationship between variables/values. Comparison operators are used inside conditional statements and evaluate to either true or false. Here are the most important comparison operators of PHP.

Operator

Name

Example

==

Equal

$x == $y

===

Identical

$x === $y

!=

Not equal

$x != $y

<> 

Not equal

$x <> $y

!==

Not identical

$x !== $y

Greater than

$x > $y

Less than

$x < $y

>=

Greater than or equal to

$x >= $y

<=

Less than or equal to

$x <= $y

 

Increment / Decrement Operators

The PHP increment operators are used to increment a variable's value and the decrement operators are used to decrement a variable's value.

 

Operator

Name

Description

++$x

Pre-increment

Increments $x by one, then returns $x

$x++

Post-increment

Returns $x, then increments $x by one

--$x

Pre-decrement

Decrements $x by one, then returns $x

$x--

Post-decrement

Returns $x, then decrements $x by one

 

 

Logical Operators

PHP logical operators are used to combine conditional statements.

Operator

Name

Example

Result

and

And

$x and $y

True if both $x and $y are true

or

Or

$x or $y

True if either $x or $y is true

xor

Xor

$x xor $y

True if either $x or $y is true, but not both

&&

And

$x && $y

True if both $x and $y are true

||

Or

$x || $y

True if either $x or $y is true

!

Not

!$x

True if $x is not true

 

String Operators

There are two operators that are specially designed for strings.

 

Operator

Name

Example

.

Concatenation

$txt1 . $txt2

.=

Concatenation assignment

$txt1 .= $txt2

 

 

Array Operators

The PHP array operators are used to compare arrays.

Operator

Name

Example

+

Union

$x + $y

==

Equality

$x == $y

===

Identity

$x === $y

!=

Inequality

$x != $y

<> 

Inequality

$x <> $y

!==

Non-identity

$x !== $y

 

 

Next Topic