English | 简体中文 | 繁體中文 | Русский язык | Français | Español | Português | Deutsch | 日本語 | 한국어 | Italiano | بالعربية

Basic PHP Tutorial

Advanced PHP Tutorial

PHP & MySQL

PHP Reference Manual

PHP Operators

In this tutorial, you will learn how to use operators in PHP to manipulate variables and values or perform operations.

What are operators in PHP

Operators are symbols that tell PHP to perform certain operations. For example, the plus sign (+The parentheses symbol tells PHP to add two variables or values, while the greater than symbol (> tells PHP to compare two values.

The following list describes the different operators used in PHP.

PHP Arithmetic Operators

Arithmetic operators are used to perform common arithmetic operations, such as addition, subtraction, and multiplication. This is a complete list of PHP arithmetic operators:

operatorDescriptionOnline ExamplesResult
+Addition
$x + $yThe sum of $x and $y
-Subtraction$x - $yThe difference between $x and $y
*Multiplication$x * $yThe product of $x and $y
/Division
$x / $yThe quotient of $x and $y
%Modulus$x % $yThe remainder of $x divided by $y

The following examples will show you the effects of these arithmetic operators:

<?php
$x = 10;
$y = 4;
echo($x + $y) . "<br>"; // Output: 14
echo($x - $y) . "<br>"; // Output: 6
echo($x * $y) . "<br>"; // Output: 40
echo($x / $y) . "<br>"; // Output: 2.5
echo($x % $y) . "<br>"; // Output: 2
?>
Test and see‹/›

PHP Assignment Operators

Assignment operators are used to assign values to variables.

operatorDescriptionOnline ExamplesIdentical to
=Assignment
$x = $y$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 (the remainder of division)$x %= $y$x = $x % $y
.=String concatenation$x .= $y$x = $x.$y

The following examples will show you the effects of these assignment operators:

<?php
$x = 25;
$y = 35;
$z = "25";
var_dump($x == $z) . "<br>";  // Output: Boolean value true
var_dump($x === $z) . "<br>"; // Output: Boolean value false
var_dump($x != $y) . "<br>";  // Output: Boolean value true
var_dump($x !== $z) . "<br>"; // Output: Boolean value true
var_dump($x < $y) . "<br>";   // Output: Boolean value true
var_dump($x > $y) . "<br>";   // Output: Boolean value false
var_dump($x <= $y) . "<br>";  // Output: Boolean value true
var_dump($x >= $y) . "<br>";  // Output: Boolean value false
?>
Test and see‹/›

PHP Increment/Decrement operator

Increment/The decrement operator is used for increment/Decrement the value of the variable.

operatorNameDescription
++$xPre-incrementIncrement $x1Then return $x
$x++Post-incrementReturn $x, then add $x1
--$xDecrementSubtract one from $x, then return $x
$x--After decrementReturn $x, then subtract $x1

The following examples will show you the effects of these increment and decrement operators:

<?php
$x = 10;
echo ++$x ; // Output: 11
echo $x;   // Output: 11
 
$x = 10;
echo $x++; // Output: 10
echo $x;   // Output: 11
 
$x = 10;
echo --$x; // Output: 9
echo $x;   // Output: 9
 
$x = 10;
echo $x--; // Output: 10
echo $x;   // Output: 9
?>
Test and see‹/›

PHP Logical Operators

Logical operators are usually used to combine conditional statements.

operatorNameOnline ExamplesResult
AndAnd
$x and $yIf $x and $y are both true, then it is true
OrOr$x or $yIf $x or $y is true, then it is true
xorXOR$x xor $yIf $x or $y is true, then True, but not both true
&&And
$x && $yIf $x and $y are both true, then it is true
||Or$x || $yIf $$x or $y is true, then it is true
!Not
!$xIf $x is not true, then it is true

The following examples will show you the effects of these logical operators:

<?php
$year = 2014;
//Leap years can be divided by400 divisible, but also divisible by4Divisible, but not divisible by10Divisible by 0.
if(($year % 400 == 0) || (($year % 100 != 0) && ($year % 4 == 0))){
    echo "$year is a leap year.";
} else{
    echo "$year is not a leap year.";
}
?>
Test and see‹/›

PHP String Operators

There are two special ones forStringOperators designed for

operatorDescriptionOnline ExamplesResult
.Chaining$str1 . $str2$str1And $str2Concatenation
.=Concatenation Assignment$str1 .= $str2Assign $str2Append to $str1

The following examples will show you the effects of these string operators:

<?php
$x = "Hello";
$y = " World!";
echo $x . $y; // Output: Hello World!
 
$x .= $y;
echo $x; // Output: Hello World!
?>
Test and see‹/›

PHP Array Operators

Array operators are used to compare arrays:

operatorNameOnline ExamplesResult
+Set$x + $yThe union of $x and $y
==Equal$x == $yIf $x and $y have the same keys/Value pair, then it is true
===Equal$x === $yIf $x and $y have the same order and the same type of the same key/Value pair, then it is true
!=Not equal$x != $yIf $x is not equal to $y, then it is true
<>Not equal$x <> $yIf $x is not equal to $y, then it is true
!==Not equal$x !== $yIf $x is not equal to $y, then it is true

The following examples will show you the effects of these array operators:

<?php
$x = array("a" => "Red", "b" => "Green", "c" => "Blue");
$y = array("u" => "Yellow", "v" => "Orange", "w" => "Pink");
$z = $x + $y; //$x and $y concatenation
var_dump($z);
var_dump($x == $y);   // Output: Boolean value false
var_dump($x === $y);  // Output: Boolean value false
var_dump($x != $y);   // Output: Boolean value true
var_dump($x <> $y);   // Output: Boolean value true
var_dump($x !== $y);  // Output: Boolean value true
?>
Test and see‹/›

PHP Spaceship OperatorPHP 7

PHP 7An new spaceship operator (<=>) has been introduced, which can be used to compare two expressions. Also known as the composite comparison operator.

If both operands are equal, the spaceship operator returns 0, if the left side is greater than1, then return1, if the right side is greater than1, then return-1. It basically provides three-way comparison, as shown in the table below:

operator<=> is equivalent to
$x < $y($x <=> $y) === -1
$x <= $y($x <=> $y) === -1 || ($x <=> $y) === 0
$x == $y($x <=> $y) === 0
$x != $y($x <=> $y) !== 0
$x >= $y($x <=> $y) === 1 || ($x <=> $y) === 0
$x > $y($x <=> $y) === 1

The following examples will show you how the spaceship operator works in practice:

<?php
// Compare Integers
echo 1 <=> 1; // Output: 0
echo 1 <=> 2; // Output: -1
echo 2 <=> 1; // Output: 1
 
// Compare Floating Points
echo 1.5 <=> 1.5; // Output: 0
echo 1.5 <=> 2.5; // Output: -1
echo 2.5 <=> 1.5; // Output: 1
 
// Compare Strings
echo "x" <=> "x"; // Output: 0
echo "x" <=> "y"; // Output: -1
echo "y" <=> "x"; // Output: 1
?>
Test and see‹/›